Web Programming TutorialsControllers and their uses in Laravel

Controllers and their uses in Laravel

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’);
    }	
    }
    ?>
    
  • 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’,’[email protected]’);

  • In general it’s syntax is given as:
  • Route::get(‘route.name’,’[email protected]’);
    Route::post(‘route.name’,’[email protected]’);

  • 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:
  • <?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
    }	
    }
    ?>
    
  • 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’=>’[email protected]’]);

  • We can even specify controllers in our controllers constructor as shown below:
  • <?php
    		class UserController extends BaseController 
    		{
    
          		   public function __construct()
       		   {
         		     $this->middleware('auth');
    
          		   }
    	             }
    
    ?>
    
  • Let us have a look at the actions handled by the controller:
  • 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.

`

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -Newspaper WordPress Theme

Latest article

21,068FansLike
4,207FollowersFollow
101,000SubscribersSubscribe

More article

- Advertisement -Newspaper WordPress Theme