Laravel Tip - Manage Huge Route Files By Creating Multiple Route Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys welcome to liverpool's in today's video we'll be looking at how we can manage our route files by default laravel comes with web.php file if you see here inside the routes folder there is web.php file and also there are other routes file so this api.php will use for creating apis but basically we'll use web.php for front-end and back-end routing and now let's say you have a lot of routes in this web.php and all the routes for front-end and as well as all the routes for the backend or admin panel that are in the same web.php file and whenever project goes this file will get huge and whenever you need to find a route then you have to just scroll and scroll of course you can do a source but wouldn't it be great if we could manage this admin routes in a separate file yes we can do that so let's see how to do that so first of all let's see what we get whenever we visit a dashboard so here guys whenever we visit a dashboard we'll see this dashboard text so if i go here inside so you can see here it is just a normal inside the dashboard and inside articles so this is just a normal okay so here we'll say here articles and it is just returning the articles now let us create a separate file inside this route start php let's say admin dot php and now let us move this code from here to here and let us also create a php opening tag and let us import the route okay now let us also import the admin dashboard controller and articles controller so now guys we have done this and we have removed it from here now if i come here and refresh the page now i will see a 404 page because by default laravel will look into web.php and it will not find that route because we have added that route in admin.psp now how to tell laravel that we also have admin.psp file so we have to go to route service provider here which is inside app providers and route service providers and you can see it over here and you can see here guys the laravel registers the routes using route service provider and it registers here the boot method and you can see the routes being registered so you can share the api.php and web.php now we know that we have created admin.psv file now we will register it from here so we'll just duplicate this web.psp because all the admin related code within the web.php we are moving to another file so we will just do this and instead of web.php will say here admin dot php so you can see here guys inside the routes here we have admin.psp and this base path will give us the path to the app folder so this app folder and after that we'll look into routes and then admin.php so now we have defined this now if i just come here and refresh the page now you can see here guys laravel can find those routes and again if i go to dashboard also then also it will find that route and also guys if you see here in the admin.php file here so here we are using prefix and it is giving us one level of indentation and we can also remove this prefix admin from here and move it to this one so let's say here prefix admin like this and we can remove it from here and if i come here and refresh the page now you can see here guys it is working the same way now look at here guys let's see you have a hundreds of routes for the admin panel then it can go here in admin.php file and every other routes that can go here in the web.psb which is related to the front end so now guys you can also add middleware so let's say you want to add specific middleware to the admin routes then you can do middleware and give the any middleweight that you want here so that's how we can manage routing in our project for larger projects and if your project is very small then you don't have to do this just stick with web.php file and if your project grows then you can just move it it will just take a couple of minutes so that's it for this video guys i hope you enjoyed this video if you enjoyed this video then please don't forget to hit that thumbs up button and also hit subscribe button if you want to see more videos like this thank you for watching have a great day bye
Info
Channel: Laratips
Views: 7,363
Rating: undefined out of 5
Keywords: laravel for beginner, laravel beginner tutorial, laravel tutorial for beginners, laravel 8 tutorial, laravel 8, laravel tutorial, laravel advanced, laravel 8 advanced, laravel advanced tutorial, laravel 8 advanced tutorial, php, php tips, php tips and tricks, tips and tricks, laratips, laravel, laravel tips, routing, laravel routing, manage routes, manage large routes, custom route files, custom route files in laravel
Id: BX1jJ6kT0Lo
Channel Id: undefined
Length: 4min 24sec (264 seconds)
Published: Fri Sep 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.