Tags

, , , , ,

Creating Controllers in Laravel is super simple. In an MVC pattern, controllers play a vital role and defining your all request handling logic as an anonymous function in route file is unthinkable for a big application.

So we always use controllers as a transport medium. The main advantage of a controller is it can group all the request handling into a single class and that class can be stored in the ‘app/HTTP/Controllers’ directory.

There are a few ways to create a controller and we can always add some functionalities into it later to use features such as Middleware.

You can create a basic controller by using this command:
$ php artisan make:controller MyController
However, for a big application such as what we are going to create, it is always wise to create a resourceful controller.

When you issue such command:
$ php artisan make:controller MyController –resource
Laravel creates a controller class that automatically adds typical “CRUD” routes to the controller. This single line of code generates all types of methods that we usually require to create, retrieve, update and delete records.
In the route file, we can also use all the functionalities using one line of code.
Route::resource(‘myfiles‘, ‘MyController’);
With the help of this single route declaration you can handle variety of actions on the resource. It now becomes easy as the controller has come up with all possible methods that we require for such actions. As the book progresses, you will get ideas how these resourceful controllers also keep informing you about the HTTP verbs and the URIs they handle.

Another advanced feature is you can register many resource controllers at once by passing an array to the ‘resource’ method. It looks like this:
Route::resources([
myfiles‘ => ‘MyController’,
moreactions‘ => ‘MoreController
]);
A resourceful controller uses many types of HTTP verbs at the same time, such as, GET, POST, PUT/PATCH and DELETE.

By using GET verb you can access four ‘action methods’ in the controller, they are ‘index’, ‘create’, ‘show’ and ‘edit’. The action methods in the controller handle the URIs and Route names in the same name, such as, ‘index’ actions handle the URI like ‘/myfiles’ and the Route name is ‘myfiles.index’. The ‘create’ and ‘edit’ actions only show web forms, so the GET verb is perfect for them.

We need POST verb for the ‘store’ action method. For that reason, the URI remains same as ‘index’, like ‘/myfiles’, but the action method changes to ‘store’ and Route name also changes to ‘myfiles.store’. The HTTP verb PUT/PATCH is used for only updating the records. Therefore, URI changes to ‘/myfiles/{myfile}’ and the action method changes to ‘update’ and the Route name changes to ‘myfiles.update’. As we progress we will see how we can use these advanced features of Laravel 5.7.

In fact, using a single command you can also bind that Controller to the respective Model. Since we have already created the respective tables and models, we are going to create only the resourceful controllers.

You can get the details in my new book:

Laravel 5.7.*: Middleware, Authentication, and Authorization Explained

You may also check the other books:

Just published! My new book on Laravel 5.7.*: Middleware, Authentication, Authorization Explained: How to Authenticate a company, projects and tasks management application.

Just published! My new book on Beginning Ethical Hacking with Kali Linux: Computational Techniques for Resolving Security Issues.

Advertisements