Learn about Laravel Framework: Routing basics

Tram Ho

For the Laravel Framework , whenever a new request is made, the web server (like Nginx or Apache) will hand over the processing to Laravel in a single entry point file (located in the public/index.php directory in the project directory). ). Each request will then be automatically redirected to the appropriate controller by Laravel, via a feature called Routing .

In this article, I would like to share the knowledge about routers in Laravel that I learned during learning about Laravel Framework.

Where is the configuration file for routes?

The route configuration files are located in the routes/ directory of your project. Specifically, there are 2 files that you need to care about now:

  • routes/web.php if you need to configure routing for traditional websites
  • routes/api.php if the route you want to create is API format

The main difference: routes/web.php has middleware that provides features like session or CSRF protection, which are not necessary for the API form. For routes in routes/api.php , addresses are automatically routes/api.php with /api .

Define a route

The simplest route can be written as follows:

Try to access localhost:8000/foo , you will have:

Minimal route

This is a very simple route, only returns the response data without going through the controller.

In case you have defined a controller (in app/Http/Controllers/ ) and want to create a navigation route to that controller:

HTTP methods (verb)

In addition to GET, you can also define routes for other HTTP methods:

Or even many types of HTTP methods at the same time:

Protection from CSRF

For routes defined in routes/web.php , you need to include the CSRF field in the following form:

Navigation

You can set a route to redirect the route to another address:

Static route (returning only one view )

With simple routes, we can let it return the view without going through a controller.

Parameters in the route

When developing a Web site, you will often encounter situations where you need to “capture” parameters on the URL and use it to display the corresponding content. For example, when a user visits https://example.com/users/1 , you need to return the information of the user whose id is 1 in the database. In Laravel, there are 2 types of parameters are required and optional.

Defining a route requires the simplest parameter as follows:

However, sometimes you may want to define a route for which a parameter may or may not be (optional). To define such a route, you just need to add the ? after the parameter name like the example below:

Result:

  • If you access /welcome then the above route will return “Welcome, my friend!”.
  • If you visit /welcome/Thang then the above route returns “Welcome, Thang!”.

Prefix for the route group

Laravel allows you to group multiple routes together into a single group. Grouping routes has many applications, and routes in the same group can be assigned with middleware, namespaces, or the same prefix.

There are many commonly used prefix cases. For example, when your site has normal paths /posts , /users , … for users, and there is a separate path for the dashboard for admin to use /admin/users , /admin/posts , .. In this case, you should create a group of admin routes and use the prefix feature for that route group.

To create a route group with the prefix for the admin as above, you can add the following:

Resourceful Route

Resourceful Route is a feature that helps us create routes of all HTTP methods, corresponding to the actions in the corresponding controller, with all operations on a data type (read, add, edit, delete, ..). .). This route helps you avoid having to define each route for each operation on a data type.

To define a resourceful route like this is very simple:

For example, for photos , the code that defines this route will create different routes, corresponding to the actions of PhotoController as follows:

VerbURIActionRoute Name
GET/ photosindexphotos.index
GET/ photos / createcreatephotos.create
POST/ photosstorephotos.store
GET/ photos / {photo}showphotos.show
GET/ photos / {photo} / editeditphotos.edit
PUT / PATCH/ photos / {photo}updatephotos.update
DELETE/ photos / {photo}destroyphotos.destroy

When using the Resourceful Route, if you only want to define certain types of actions, or exclude certain types of actions, you can define the following:

Conclusion

Above are the basic features of Routing in Laravel. To learn more about Laravel Routing, please read the official Laravel documentation. Thank you for reading the article na!

Share the news now

Source : Viblo