Controllers and their uses in Laravel

0
2443

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:
  • This is a controller. After writing a controller there is a route entry done to register a route for controller action.
  • The route entry is done as follows:
  • Route::get(‘about’,’indexController@index’);

  • In general it’s syntax is given as:
  • Route::get(‘route.name’,’somecontroller@someaction’);
    Route::post(‘route.name’,’somecontroller@someaction’);

  • Let us know about different types of controllers:
  • 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:
  • 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:
  • Registering the route is shown below:
  • Route::controller(‘index’,indexController);

  • In general to register route to a RESTful controllers we use
  • Route::controller(‘route.name’,’ControllerName’);

  • Controller middlewares:
  • Middlewares may be specified on controllers route. It is shown below:
  • Route::get(‘profile’,[‘middleware’=>’auth’,’uses’=>’UserController@Profile’]);

  • We can even specify controllers in our controllers constructor as shown below:
  • Let us have a look at the actions handled by the controller:
  • HTTP VerbAction(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.

`

LEAVE A REPLY

Please enter your comment!
Please enter your name here