Laravel - Day 5 - Defining controller and working with views and layouts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello what's up everybody how is it going welcome to another video of tutorial series learning laravel from scratch I'm your host unwarily and I'm here today with the new topic and the topic is understanding Lara fans view and some some of the basics of blade template okay then so in the last video we covered some of the basic stuffs like creating the migration running the migration creating the migration along with the model and those other thing that we created last time so today before starting with the blade template or V what I want to do is I want you to understand the concept of routes which is over here and the concept of controller that will be creating shortly so currently we don't have we only have [Music] inside controller we only have home controllers so simile and just like home controller we would like to create a new controller and you can just call it for now test controller okay so let me just quickly open my terminal so in order to create the controller inside Unbearable project what we need to do is first of all it is go get inside the project folder which is in science class applications amps w-w-w project and run the command PHP artists and make just like Moodle will say make controller this time and after this will give me giving our controllers name and for now I would just like to create a controller called test controller so for Narvel we should write the name of the controller and follow it by capital c ont are our control this is how we create the controller in our so this should always be camel kissing the first letter of the controller and the first letter of the name of the controller and the controller itself should be capitalized so test capital T controller capital C after we do this it will say as controller created successfully now if we go back to our sublime text and see you will find a test controller over here okay let me create a function public function index just write a hello world over here echo hello world okay so this is the controller that we just created we created a function inside the controller test controller which is hello world I mean index and we print it out hello world inside it one of the amazing thing about laravel is that in order to make this controller and function workable we need to write routes for it for each and every URL to work in laravel should have its own routes mentioned inside web dot PHP so this is and this is an example of route which says whenever there is a hole in the URL after a tab base URL and we are trying to browse that you are through get which means nothing is being posted nothing is being patched or anything so just just a lot normal browsing in that case we use get it is just like the forms get method to post method we will have some more types of route in laravel which we'll discuss shortly but just if we have home in the URL this means homes home controllers index function so this is the routes for slash home and this name is just an alias or a shortcut a shortcut name for this route so this is a short name for the route so whenever we we want to browse there we want to get the and the full URL you can just give this a name to the route function and it will generate as the food route which we'll discuss about later on but for now you should just take care of this so this means route get if there is a home in the URL this mean home this means home controller and index function so if I just go and type in here home it will take me to login because I'm not yet logged in the but we will be creating our own routes for test controller so I'll just copy this the same thing gets it over here so if I see if there is a test in the URL this means test controllers index function so I'll just write test now if I try to browse this by typing in test over here you will see I got hello world because this whenever we type something in the URL it first of all checks if the Royal is present in the rumps and it it finds it over here so the here is the test did and it checks in a controller name and the function name for that URL which means test controller and index function and it's Elias is let's say test okay you should not have same ideas for two routes otherwise it will replace it or overwrite it so for our test controllers index phones and we have defined our route index we can even define our own routes for example ASD let us say if there is ASD in the URL this means test controller and index function let us see ASD in turn it's saying hello world so route I just wrote test over here because it is always good practice to have naming convention name of the controller name of the route similar to what you are doing so it is better good practice to have the naming convention of the URL and make it a user friendly okay similarly if you see this is me and this means that if you type in test it works but if we type just controllers index function just like in code igniter it won't work anymore because we haven't written the route for tests last index but now if we go and write tests last index over here this will work but test will work tests will not work anymore because there is no you are an on there is no route mentioned for only test we can also have multiple routes for the same controllers and same function so let us suppose if somebody in a discrete or route first control-c control-v remove this so if there is a test in the URL only test this means test controller index function and if there is tests less index this also means test controllers index function and you can say test 1 and test 2 the name of the routes so now if you type test it will work because just means test controllers index function and test /index this will also work because this also means test controller index function so we can have two different routes for the same function and the same controller ok so I hope this is not much complex for you and so this is the an easy way to define a route we will deal about the name later on so for now test means test controller is index function okay okay now let us say we want to create another function inside test and name it like public function about let's say echo this is about okay now we type in here about it will not work because we have not yet defined our hops for about so what we can do is we can just copy the same route paste it over here and just say about which means disk controllers about function and we can just call this about now refresh this and he is now working so this was a simple concept of defining routes and defining the control functions inside controller and in order to make it workable the on each and every function should have its own routes after that it will work fine okay so we generally don't echo or print out something inside the URL rather we load the view from the controller right so let's suppose we want to load a view called home inside our index function so in order to that we'll just say return view and give the name of the view let us say you would like to create a view called test okay and for this we want to create another view you should do it later on but for now let us say return view test so now this index function of the test controller should return a view so to create our view we should get inside our resources control folder and there's a views folder over here and we can create our view inside our views folder so let us create a view contest test now in order to since laravel uses blade template now our naming convention for the view should be the name of the view dot bleed dot PHP okay so let us create a new file and call it test dot blade dot PHP so we have created our view file successfully now let us try try to write something over here let us say this is test page and save it now as per our routes whenever we type in test it should call test controller's index function and which is here so test controllers index functions now should return a view called test which is over here and inside our test we have written test this is this page so whenever we we type in test in our URL now this should show us this is test page all right so we can see this is paste test page is now here in our page so this is how we load the view from our controller using laravel there is another concept of view inside laravel and unlike encode ignitor we load our header separately load our footer separately and load our content separately inside in while working with laravel we generally work with layouts so if you see there is another folder called layouts here and we can create our own layouts and load our content through the view to that layout for example let us say we browse any site for example let's say news.com this is something that we are currently working so if you go to this website you will see there is a header and there is a footer okay which should be repeating in each and every page so for example if I'm here in another tab you will see the hem hit hits the same header is on the top and the same footer is on the bottom if you go to that any detail page of the news you will still see the same header on the top and the same footer in the bottom so this means there is something that is being repeated in each and every page that is our header and that is our footer so this thing this repeating thing we generally put inside our layouts folder this means each and every view should share the same layout with the same header of the same sidebar and the same footer the only thing that that should be changing is our main content of the body which is in the left side right so the image or there is and this title and the description and some similar pools these are the thing that should be changing otherwise we should have a same header same sidebar in the same footer so in order to introduce this while we worked in the core ignitor we had written a core separate code for the header a separate separate view file for the footer and separate view file for the sidebar and we used to load each and every view separately in each and every views right but in laravel we have a concept of layouts where we create our own layouts and start working on this so AB dot bleed or PHP is a default layout that is generated by the laravel itself so if you go and see in the app app drawer blade or PHP layout you will see the there are some of the for example for the hater stuff we have a Steven open head open body open we have some navigations and finally here this is the part for the main content and we have div close body close everything right so just quickly create our own layout and try to use that in our test plate okay so let us create our new layout and let's say you want to call it like my layout don't bleed dot PHP so this is the place place where we'll be creating our header footer which should be repeating in each and every page so let us say we have HTML and my title will be laravel project okay and inside my body let us say I want to have a deep four header let us say this is for header so I just name it header and let us say I just write a code call this is head out over here and we have another deal let us say this is for footer div close let's give a class called footer and let's say this is futa and here I want to load the content of my view files that I am loading through controller so for example my test controller is loading the view call test and inside test we have this is test page and I have one this content to be shown over here in the middle inside paragraph tag okay so this is the place where my content should be shown and this is the place where my header should appear this is the place where my footer should appear so what I generally do is I just place a function it's called yield here so we are using blade templates so one of the way to call the functions in blade template is placing adroit in front of it and call the function yield content okay so this is the place where I want to show the section called content so yield is a function that actually loads the sections so they I'll explain about sections shortly so in blade template what we do we generally create sections inside our view folder and we load those sections using the lead I mean yield function in our layout so right now I'm just creating a function called yield and trying to load the content called I mean section called content so this section content should be defined inside our displayed so let's say here now in order to use this layout in my test blade what I need to do is I need to extend this layout so I I need to call the function called extends and I need to give the name of the I need to give the path of my and this layout which is my layout update dot PHP so my part is inside views folder I have layouts folder so I need to get inside layouts and give a slash which means in gray template we generally do not slash with and dot so I need to get inside layouts and I need to I need to give the name of my layout which is my layout okay I've explained about it and after that we need to define the content or section that we are loading inside our template or layout so in the name of the section that I'm trying to that I'm trying to load is content right so I need to define a section called content inside my view file so section and since section is a function I need to define the name of the section inside the bracket so my sex in the name of the section that I'm using in my layout is contained so I need to say this is the section called content and I want to load my texts [Music] inside the content I would I want to define something like let's say this is test page after this I want to end my section so in order to in the section I just need to say end section okay so let's see I'll explain about this in detail but let us just quickly see if everything worked fine so whenever we call test it says the out of my house was not found so which it's not my layout it's my layout so inside my test it is not layout it just lay out okay now let's try this is saying this is a header this is footer and this is test page it it has an extra semicolon at the last because I inside my layout I have a semicolon over here and we don't need semicolon in blade template you can see this is header this is footer and this is the content that I'm sending so what's happening let's see so inside our routes we have said that test means test controllers index function so it goes inside test controller and it calls the index function an index function is saying to return the view called test so it is now checking for the view name test and it finds it over here and this test is saying that it wants to extend the layout called layouts called my layout which is inside layouts folder so it grabs the content from my layout which is my under the layout manager that I just created and it is this layout is expecting a content or a section called content right and so yield is a function that loads the section which is defining the view so this layout is expecting section called content so it now searches for the section called content inside test plate or this view so it finds that the section is defined over here that section called content is defined over here so it grabs the content of this section which is start which is over here and copies it and place it places it inside this place so what it does is does is it grabs that the content which is defined inside the section and places it over here so we have this content on the top this content on the bottom and this is in the middle so that's why we caught this is header on the top this is footer in the bottom and this in the middle so this is how loading of the views and and using the content sections in the layouts so works in the learner now if you see let us say I want to use the same layout inside my about portion as well so let us just return a view called about whenever we browse about so we already have a route for about which is disk controllers about function and this is test controllers about fortune which is loading a view called about so we need to create a view called about inside our views so new file control s about dot bleed dot PHP and sins and even this function should also use the same layout so I need to extend the same layout extends and I need to give the path to my layout which is insert layout folder dot my layout right now and you define the section which is over here so this layout which I'm extending is expecting for a section called content so I need to define a section called content so let us define it so section and the name of my section is content so for about I want to place a let's say let's say I want to have a heading tag let's say about us and I want to give a paragraph tag and say this is about page and I want to end a section right here so end section so now what it does it it grabs the layout so it is grabbing each and every part of from from here to here but instead of this yield content now it's it sends the section called content from the view which is inside my about from here to here it will send this content to my layouts this block okay so pack a heading tag and paragraph tag will appear over here and it will have this on the top and this on the bottom so what we should see now is this is a header on the top and the heading tag and a paragraph tag and this is footer in the bottom right so let's try let's try to browse and browse about and you will see maybe we mistook it somewhere so inside my about blade so extents not intense so its extents right let's try to the fish this now you will see this is header is in the top this is footer is in the button and the content that we sent was a section that we sent was about us hate in h1 time and this in the paragraph time so this is exactly what we got so this is how we generally work with Lara veins view and Lara veins layout defining the section and sending the content of that section to the layout and loading the layout loading each and every section internal layout so this is how and we exactly work with Lara will be in controller so we will be using a new template maybe a good design of the template and we will try to create difference and different sections for our template maybe a section called header or system called sidebar section called footer and we can use they use those sections in out and layout so we will do that in coming videos but this was just a basic briefing about the concept of layout and templating and let's say the view and the controller and the routes we will learn more about different types of routes how to define controller in in an appropriate way you'll also learn about how to use the views and lay template in more bit away for example using how do you if else conditions using plate template how to use loops and how to print out something so they have various things we need to discuss more but that was it for today we will be discussing more on this in coming videos and I hope you understood on the video and you liked the video and if you did don't forget to click on like button subscribe it and comment on it and I'll see you guys tomorrow with the new topic till then have a nice time [Music]
Info
Channel: Kodiary Technologies
Views: 8,940
Rating: undefined out of 5
Keywords: PHP, Laravel, Laravel from scratch, Laravel from basic, Learn Laravel, Laravel Controller, Laravel Views, Laravel Layout, Kodiary, Kodiary Technologies, Anwar Ali
Id: hXbxOj6D-R8
Channel Id: undefined
Length: 24min 36sec (1476 seconds)
Published: Wed Apr 25 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.