Today we will study about the controllers used in this Controllers and their uses in Laravel tutorial.
- Controllers in any MVC architecture have been assigned a task of controlling the traffic between views and models. Controller is used for every resource of your application.
- Instead of defining all route-level logic in routes.php file, we can organize it in a controller class. Controllers can group related route level logic into a class.
- Controllers are stored in the app/controllers directory. This directory is registered in the classmap option of the composer.json file by default.
- All the controllers should extend the BaseController class. This class also resides in the app/controllers directory.
- Let us see how to write a controller class:
<?php class indexController extends BaseController { public function index() { return view::make(‘about’); } } ?>
Route::get(‘about’,’indexController@index’);
Route::get(‘route.name’,’somecontroller@someaction’);
Route::post(‘route.name’,’somecontroller@someaction’);
- Implicit Controllers:
- Instead of defining multiple routes Laravel allows us to define a single route to handle every action in a controller.
- First we need to define a route using the Route::controller method.
- The controller method accepts 2 arguments, first is the base URI that the controller handles and second is the class name of the controller.
- For example: Route::controller(‘index’,’indexController’);
- Here we have to add methods to the controller by just prefixing them with HTTP verbs as shown below:
<?php class ShowController extends BaseController { public function getIndex() { // } public function postProfile() { // } } ?>
- RESTful Controllers:
- REST is simply a set of rules that govern how data transfer should happen in web pages.
- REST defines certain methods that are applicable for almost every type of resource in web application.
- REST is also a fundamental part of how internet works and so it makes creating web applications much easier.
- We can create RESTful controllers by prefixing controllers action name with HTTP verb as shown below:
<?php class indexController extends BaseController { public function getAction() { //get request handling } public function postAction() { //post request handling } } ?>
Route::controller(‘index’,indexController);
Route::controller(‘route.name’,’ControllerName’);
- Controller middlewares:
- Middlewares may be specified on controllers route. It is shown below:
- We can even specify controllers in our controllers constructor as shown below:
Route::get(‘profile’,[‘middleware’=>’auth’,’uses’=>’UserController@Profile’]);
<?php class UserController extends BaseController { public function __construct() { $this->middleware('auth'); } } ?>
HTTP Verb | Action(Method) | Route Name |
---|---|---|
GET | index | resource.index |
GET | create | resource.create |
POST | store | resource.store |
GET | show | resource.show |
GET | edit | resource.edit |
PUT/PATCH | update | resource.update |
DELETE | destroy | resource.destroy |
Thus we studied about laravel controllers in this Controllers and their uses in Laravel tutorial.
`