Brief Summary
This video is a concise guide to Laravel routing for beginners, covering essential concepts and syntax. It explains how to define routes for static pages, handle dynamic data with URL parameters, use route model binding for cleaner code, and leverage route names for maintainability. Additionally, it touches on route groups, middleware, resource controllers, and API routing, providing a solid foundation for understanding Laravel's routing system.
- Defining routes for static pages using
route::view.
- Passing dynamic data with URL parameters and route model binding.
- Using route names for easier maintenance and refactoring.
- Grouping routes with middleware for applying authentication or other filters.
- Utilizing resource controllers for CRUD operations and API routing.
For static pages represented by Blade templates, you can directly point a URL to the view using route::view. Instead of using route::get with a callback function that returns the view, route::view offers a shorter syntax. You provide the URL and the name of the Blade file, which simplifies the route definition for static content.
When your Blade template requires dynamic data from the database, you can pass parameters in the URL. Define the parameter in the route using the /user/{id} syntax and point the route to a controller method. The controller method should accept a parameter with the same name, perform the database query, and return the view with the data. Laravel also offers route model binding, where you can directly inject the model instance into the route, eliminating the need to manually query the database.
Route model binding simplifies the process of injecting model instances into routes. Instead of passing the ID and manually querying the database, you can specify the model variable in the route definition. Laravel automatically resolves the model instance based on the ID in the URL. This feature cleans up your code and reduces boilerplate.
Assigning names to routes allows you to reference them throughout your project using the route() helper function. This is beneficial for generating links, redirects, and other URLs. If you later decide to change the URL structure, you only need to update the route definition, and all references will be updated automatically. Route names also provide IDE autocomplete, reducing the risk of errors.
For controllers that represent a full CRUD (Create, Read, Update, Delete) resource, you can use route::resource to define all the necessary routes with a single line of code. Laravel automatically generates route names for each action (index, create, store, show, edit, update, destroy). Route model binding also works seamlessly with resource controllers in the show, edit, update, and destroy methods.
Route groups allow you to apply middleware to multiple routes at once. This is useful for applying authentication, authorization, or other filters to a set of routes. You can define route groups within the same routes file or separate them into different files for better organization. Laravel Breeze, for example, separates authentication-related routes into a separate file with specific middleware.
When building an API with Laravel, you can use the routes/api.php file to define your API routes. To activate this file, you need to run php artisan install:api. API routes are prefixed with /api by default. This ensures that your API endpoints are separate from your web routes.
Summarize any YouTube video in seconds
Get summaries like this one for any video, in your language, with the free Summ app.
Transcript
Full transcript of “Laravel Routing: Explained in 3 Minutes”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.
0:00 Hello guys, I need your opinion on something, my new experiment. So now you will see a threeinut video about routing for beginners in Laravel and I feel the need for such shortened video about the main first things they need to know because if you open any Laravel documentation page at the very beginning, it feels pretty overwhelming with a lot of syntax options. So you wouldn't even know which ones you would use most of the time. So please watch those three videos and tell me, should I continue with these summary videos? I'll probably put them on the website Laravel Dailycom as a part of the road map. So here in the road map there are a lot of separate topics and many of them deserve I think summarized version like the topic explained in 2 3 minutes. Let's discuss in the comments below. This is Laravel homepage represented by
0:47 welcome.blade.php file. So if you have a static page represented by just blade, you can point to it with route get URL for example welcome and a callback function that returns the blade view. So now if we go to /welcome it will show the same welcome blade but for static blades there's a shorter way just route view and then you provide the URL for example W for welcome and the blade file and now our welcome would not work would show 404 but our W route would show welcome blade. Next case, if your blade has some dynamic data from the database represented by the URL parameters like / user/1 but in the blade you want to show the username queried from the database. So in the route you pass the parameter
1:33 with this syntax and you point that route to the controller and its method. That controller method has to have a parameter with the same name and then you perform the database operation and return the view with passing the data like this. Also, Laravel allows a shorter syntax for these cases which is called route model binding. So, instead of passing the ID, you provide that this should be user model variable called user. Then in the routes, you provide that it's user variable name. And then you don't need to call find or fail. That user is filled in automatically. It's also important to add names to routes like in this case which allows you to reference that URL with route name in the links in the redirects and elsewhere in your project. So here's an
2:20 example of two links. This is without route name and this is with route name. So if at some point you decide to change the URL throughout all your project, you would have to do a lot of manual changes in all blade files and elsewhere. Also, as you can see, it doesn't have IDE autocomplete, so it's easier to make mistakes. That's why it's better to assign routes and use them everywhere. Next, if your controller represents the full CRUD or so-called resource with index create, update, and delete actions, you can put the route to it with only one line, route resource, and then Laravel takes care of route names automatically. And also route model binding is working like this in show edit and update and destroy methods. Finally, important to understand route
3:05 groups and middleware. So here's an example from Laravel Breeze starter kit. You may group a few of the routes into route group and assign middleware off. And you can create separate groups within the same routes web file or separate those into a separate as in case of Laravel breeze they separated that into routes o file with separate route groups and middleares. The final note is about API routes. So if you're working with API project with Laravel you need to run PHP artisan install API which would activate the routes API file. So this is our routes API. This was generated by Laravel and this is our custom route and it returns JSON. And what you need to understand is that API by default is prefixed with slash API. So we send and we get that response. So
3:52 whatever you put inside of routes API should be prefixed by default with slash API. So these are in my opinion the main things that you need to know about Laravel routing. For everything else and all the other syntax options, read Laravel documentation.