How To Do Routing In Laravel 5

By | November 27, 2015

728x90-popads_1

How To Do Routing In Laravel 5

Laravel is a brilliant PHP Framework that’s equipped with tons of interesting features including RESTful routing, native PHP or light weight tempting engine and many more. Built using several Symfony components, Laravel offers you web application an amazing foundation of reliable and well-tested code.

How To Do Routing In Laravel 5

If you have worked with the Laravel framework you probably know that you can set up your routes in the following way:

However, if you use such a method your routes.php file will get hard to maintain and unreadable very soon.

Therefore, you may refactor the routes to handle the logic inside the controller like this:

This will call the hello method on the BlogController whenever a GET request is made to example.com/hello/someRandomString.

Nonetheless, if you have user-controllable resources on which CRUD operations have to be performed it is best to use RESTful Resource Controllers. Using them, you follow a convention, clean up your routes file and always know what route does what.

To indicate that you will be using Resourceful Routing you only type line in your routes.phpfile instead of adding routes:

Then, your app would be expected to have a controller in charge (here it is BlogController) which has public method with certain predefined names.

After you have defined resourceful routes in routes.php, Artisan will help you create a template for the RESTful controller.

If you navigate in your Terminal to the project’s folder and type php artisan make:controller CONTROLLERNAME, Laravel will set up a Resourceful controller for you, with all necessary methods set up for you to start filling.

Figure-1-Creating-a-RESTful-Resourceful-controller-in-Laravel

Figure 1: Creating a RESTful Resourceful controller in Laravel:

To ease you even further, it is possible to see all the routes that were created with the above actions so you can know how your web app should be crafted (forms, redirects and so on).

If you type php artisan route:list you would get all established routes for your app, including those set up using Route::resource.

Figure-2-Creating-a-RESTful-Resourceful-controller-in-Laravel

Below, you can find a table which explains the different routes and what they are expected to do according to the convention.

Request method Route Description
POST Blog Stores a new article in the persistence layer (database)
PUT Blog/{blogEntry} Updates the current record of an article in the database
DELETE Blog/{blogEntry} Deletes a particular article (its record in the database)
GET Blog Shows all articles
GET Blog/create Displays a form to create a new article
GET Blog/{blogEntry} Shows a particular article corresponding to blogEntry – it could be an Id, a slug or other unique identifier of the article
GET Blog/{blogEntry}/edit Shows a form to edit a particular article

Now, our controller has all those actions waiting to be filled with the functionality of the resource you desire!

Figure-3-Creating-a-RESTful-Resourceful-controller-in-Laravel

That’s All Guys

728x90-popads_1