Laravel 9 Views, Layouts & Routes Tutorial | Laravel 9 Basic Routing | Laravel 9 For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to tech tool india in this video i am going to explain you about laravel roots views and layouts i have already installed arable 9 and installed laravel ui package for authentication so i have already installed the laravel authentication scaffolding as well if you want to know how we can install laravel 9 and make this authentication scaffolding you can find the video link in description and watch there so without wasting any time let's get started so i have already registered a user let's get login into the system as soon as you log in you see this is a dashboard it's displaying you are logged in and in the root if you see it's saying like slash home so if you go to the code how it looks like the folder structure is so we need to find our roots first so roots for the website will be inside roots folder there is a file called web.php you need to open this file and you will find out there are three routes this is a default one this routes are by auth scaffolding level ui package which we have installed and this is the home road so to define any route we need to use this root facade and this facade have generally four methods so after root facade we need to define which method is is so there are four kind of method we use in the roots get to get the any piece or data post to post any data into a controller put for updating any data and delete for deleting any data so these four kind of request roots which we can define here in root five so roots this is a get method and after that we need to define a uri so as you see in our after our domain name there is slash home so this is a uri the next thing which we need to define is which controller it is going to access so as it's defined the absolute part of the controller for home controller it's inside app http controllers and then home controller class so if you go to the folder open the app folder there is http there is a controllers and then a home controller so you see there this is the absolute part of this home controller class and after that it actually tell us like which function is going to be accessed inside this controller class so it's it's accessing the index function so if you go to the index function what it does it actually returns the view of home so this is actually returning a blade file which is named as a home so we will see that and after that it actually defines the name of this roads so by identifying the name we can use this root by name the advantage of using this name in future if we have changed this path and we are accessing our root by the name so it will work even though if we change this path so that's the best way to practice practice by using name so if you go to the controller you see we are returning a home blade file let's see where we can find this home file let's close it out go to our resource folder inside resource will have view folder inside view we have this home dot home.blade.php in this blade file as you can see we are extending a layout file which is inside the layouts folder we have this app.blade.php so we extended this file here and added this section container section where we are just displaying a dashboard as a card header and this is a kind of alert status if there is any alert this will alert there and there you are logged in so if you go to the view file you see there is a card which has the dashboard as a card header and then you are logged in let's change it let's add some text here and see if it is changing refresh this piece yes so we are on the correct page so how the layouts work we can understand here so basically what it does it actually you see this header if there is any footer it will be in the footer so what it does actually in the general application what what changes is the content of the page generally header and footer remains the same so what we can do we actually make a can make a layout layout file which holds all the html structure and it also yelled at some content that content goes between the header and footer or anything i mean we can extend any content anywhere but in general this is the practice which we use for layouts so the layout file will be be the same for all the pages but generally what change is the content of the page so just like this let's make a test layout and let's see if we can make another so i'm just copying this same line and paste it here and called it a new home right and instead of calling index function i called a new home function and instead of giving it a home i have given it new home so let's we need to create this function inside this home controller class we go here and simply i'll write a function here which will a new home and it will written a new file so inside our view we can make a file directly inside a view folder or we can create a folder here and then create files inside the folder creating a folder is something which we can do when we need to organize our code in such a way that we don't want to mesh a lot of file in the single folder so let's create a folder to dashboard and inside the dashboard let's call new home new home dot dot php so this is our new home template so as we are using the blade template in laravel so every view file will have dotblade.php file to identify level like it's a blade template so we can use a lot of blade directive inside the blade template let's let's put it there so what we need to do here we need to extend our layouts dot app file and then we need to start a section here called contained and and section so inside this whatever we will put it will goes to the new home page so what we can do we can simply copy this container and paste it here call it new dashboard and here we can update the content welcome to [Music] the new home page right so we have our new home page and inside this new home function what we can return we can return the new home so let's add this dashboard dot new home is the new home uh new homepage file path so this will view will actually access the file inside this resources view folder and inside that if you have any file or folder if you have a direct file you can give a direct file name without.blade.php and if you have such folder so you can concatenate with dot and then you can access the files inside the folder so that's it let's go to our browser and see if we can uh edit this to new home what we'll see new home you see as soon as we go to this new home we see a new dashboard and welcome to the new homepage for example let's say if we want to link the home page and new home page on the navigation so since the navigation is come under the layout so let's go there inside app.blade.php we see this later actives guest else so the guest directory is whenever you are not logged in you see this login and register option and once you are logged in you see this you know log out button from the top if you go here there is a logout button let's add here inside the left side of navbar let's add a auth directive inside auth directory whatever we write it will be visible only if you are logged in so let's take a li and put it here give it a class of nav item and inside a nav item will take anchor file anchored attribute of html and put the href of new homepage so as we need to put link of new home base so you know we have already given a name to the new home which is new home so how we can do this we can put a route root function inside the root function what we can call we can call any root name so as you see it's auto suggesting but what we can essentially do is new pass the new home we can call new home here and give it a name called new home let's save it and go to the page refresh this piece you see there is a new home but when i click on this you see there is a home page so home page is this as soon as you click on this new home it's going to the new home page let's add one more link here just let's copy the same and call it home and instead of new home give it a home save it go to the browser refresh this now you see there is a home and there is a new home so you can visit in between the home and new home by creating this navigation links now as soon as you logged out you see if you go to this same layout but you're not able to see that new home and home links here because we have put that file that links inside this auth directive so all the directives only will be written true once if you are a login if you are not logged in it will written false and you will not able to access any of the content put it inside this auth directive so this is the concept of layout and for accessing any view file we need to have file inside this resources views folder to organize it better way we can create multiple folders folder inside folder and then put our blade files there and wherever we see there is a reusable component we can always put there in a separate file and use that file anywhere inside a blade file so that's how we organize our views and layouts and for roots we always use roots here in for web we use roots web.php for creating any apis we put everything inside roots api.php so i hope this video is informative for you if you're new to the channel please do subscribe our channel to get more videos on laravel till the next video keep watching keep learning thank you for watching [Music] you
Info
Channel: TechTool India
Views: 2,990
Rating: undefined out of 5
Keywords:
Id: rDbsaA7bPMU
Channel Id: undefined
Length: 14min 10sec (850 seconds)
Published: Mon May 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.