Laravel Controllers

Controllers act as ‘C’ in MVC Framework. Laravel framework is based on MVC (Model View Controller) architecture. A controller is a class which contains business logic and works as a mediator between model and a view. The controller class interacts with database and give requested response to the view.

In laravel all controllers are stored in “app/http/Controllers” directory.

Creating Controller in laravel

We can use artisan command to create a controller in laravel. I am using Visual Studio Code editor for this example tutorial. Open a new terminal in Visual Studio Code and type below command.

Above command will create a class EmployeeController.php in “app/http/Controllers” directory.



We can see EmployeeController class is created in the “D:\laraveltestproject\laravel\app\Http\Controllers” directory.

<?php

   namespace App\Http\Controllers;

   use Illuminate\Http\Request;

   class EmployeeController extends Controller
   {
     //
   }
?>



By default the controller in laravel extends Controller class. This class does not have any function defined. To create a laravel Controller class with basic functions type below command in VS Code terminal.


The above command will create a new controller in the “D:\laraveltestproject\laravel\app\Http\Controllers” directory.


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Postcontroller extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}


When we create a resource controller class it by default contains functions which are used to perform basic actions like insert ,update ,delete and view.

public function create() – It is used to create a new resource. A resource refers to a new record.

public function store() – It is used to store the resource in the database.

public function update() – It is used to update the resource already stored in the database.

public function destroy() – It is used to delete the resource from the database.


Routing with Laravel Controller

We can implement Routing with laravel Controllers. when we define routes with controllers we create classes with methods and the class methods handle the route request.

We will use EmployeeController class that we created above to implement routing controller.

-> Open web.php and create a new route as explained below-

Route::get('/employee','EmployeeController@index');

Here we have defined a new route ‘/employee’ the URL to access this route will be “http://127.0.0.1:8000/employee”. EmployeeController is the controller we are using for this route and ‘index‘ is the name of method we define in the EmployeeController.php file. In the route definition @index means index method of EmployeeController will be used when we access ‘/employee’ url.

Edit EmployeeController.php file and define index method as explained below-


  <?php

   namespace App\Http\Controllers;

   use Illuminate\Http\Request;

    class EmployeeController extends Controller
    {
        public function index(){
           echo "This is index method of EmployeeController."
    }
  }

?>


-> Visit http://127.0.0.1:8000/employee the output from employeeController will be as follows.



Controller Namespaces

RouteServiceProvider loads all the route files available in the namespace. When we define routes with controller methods we do not need to specify full namespaces where controllers stored. RouteServiceProvider will load all of them. We only specify the classname and the method name.

Route::get('/employee','EmployeeController@index');

protected $namespace = 'App\\Http\\Controllers';

The above line in the RouteServiceProvider loads all the controllers in the namespace App/Http/Controllers/ so we can directly use classname in when defining routes.



Passing parameters to Controllers

We can pass url parameters to the controller methods. In web application it is a common practice to pass parameters in urls. These parameters are further used in controllers methods.

Lets say we have a route ‘/employee/100’ here 100 is the employee id. we will pass this employee id in the url and use it in the controller method.

Define a new route in web.php.

Route::get('/employee/{employee_id}','EmployeeController@index');

This employee id is now accessible in the index method of EmployeeController.

  <?php

   namespace App\Http\Controllers;

   use Illuminate\Http\Request;

   class EmployeeController extends Controller
   {
       public function index($employee_id){

           echo "Employee ID =".$employee_id;
       }
   }

  ?>



Controllers with single Action

We can create controller with single function (Action). When a controller is supposed to have only one action then we can use single_invoke method in the controller.

Lets create a controller first using php artisan command.


 <?php

    namespace App\Http\Controllers;
    use Illuminate\Http\Request;

    class PersonController extends Controller
    { 
        //
    }
 ?>

Lets define __invoke() method in PersonController.

 <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

   class PersonController extends Controller
   {
    
        public function __invoke($person_id){
        
           echo "Person ID = ".$person_id;
       }
   }

?>

Define a new route in web.php

Route::get('/person/{person_id}','PersonController');

Enter URL http://127.0.0.1:8000/person/10 following page will be displayed.

When we do not define a method name in route then we must have __invoke() method in the controller.

This is all about controllers in Laravel. In further tutorials we will see additional features of controllers provided by laravel. i.e Resource Controllers.

About the Author: allcodebuzz

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *