Laravel Controllers

Laravel Controllers

Laravel controllers are an essential feature in a Laravel framework. Initially, we were handling the request logic in the form of closures in route files; now, in place of using closures in route files, we use controller classes. Controllers are used to handle the request logic within the single class, and the controllers are defined in the "app/http/Controllers" directory. Laravel framework follows the MVC (Model View Controller) architecture in which controllers act as moving the traffic back and forth between model and views.

The default file of controller is available in the app/http/Controllers directory.

 
  1. <?php  
  2. namespace App\Http\Controllers;  
  3. use Illuminate\Foundation\Bus\DispatchesJobs;  
  4. use Illuminate\Routing\Controller as BaseController;  
  5. use Illuminate\Foundation\Validation\ValidatesRequests;  
  6. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;  
  7. class Controller extends BaseController  
  8. {  
  9.   use AuthorizesRequests, DispatchesJobs, ValidatesRequests;  
  10. }  

In the above code, the namespace is used as it allows you to use the same function names and classes in the different parts of the same application. For example,

 
  1. namespace App\Http\functions1;  
  2. namespace App\Http\functions2;  

Suppose we have to run the function having the name, i.e., RunQuery(). They are available in different directories functions1 and functions2, so we can say that namespace avoids the collision between the same function names.

'use' is used to import the class to the current file.

Let's see how to create the controller through Git Bash Window.

Step 1: Open the Git Bash Window and type the command "php artisan make:Controller PostsController" in Git Bash Window to create the Controller.

Laravel Controllers

The above screen shows that the controller named as PostsController has been created successfully.

Step 2: Now move to your project and see whether the PostsController file has been created or not. The path of the file is:

C:\xampp\htdocs\laravelproject\app\Http\Controllers

Laravel Controllers

The above screen shows that the PostsController file is created.

The default code of PostsController.php file is given below:

  1. <?php  
  2.   
  3. namespace App\Http\Controllers;  
  4.   
  5. use Illuminate\Http\Request;  
  6.   
  7. class PostsController extends Controller  
  8.   
  9. {  
  10.       
  11. //  
  12.   
  13. }  

The above code contains the class that extends the Controller class, but this class does not contain the functions such as create, update, or delete. Now we will see how to create the controller which contains some default functions.

To create the Controller, we will first delete the PostsController.php from the project, which we have created in the previous step.

Type the command:

php artisan make:controller -resource PostController, this command is used to create the controller.

Laravel Controllers

Now, move to your project to see whether the PostController file has been created or not. The path of the file would be:

 

 

C:\xampp\htdocs\laravelproject\app\Http\Controllers

Laravel Controllers

The above screen shows that PostController file is created successfully.

The default code of the PostController.php file is given below:

  1. <?php  
  2. namespace App\Http\Controllers;  
  3. use Illuminate\Http\Request;  
  4. class PostController extends Controller  
  5. {  
  6.     /** 
  7.      * Display a listing of the resource. 
  8.      * 
  9.      * @return \Illuminate\Http\Response 
  10.      */  
  11. public function index()  
  12. {  
  13.         //  
  14.  }  
  15.   
  16. /** 
  17.      * Show the form for creating a new resource. 
  18.      * 
  19.      * @return \Illuminate\Http\Response 
  20.  */  
  21.  public function create()  
  22.  {  
  23.    //  
  24.  }  
  25.   
  26.  /** 
  27.      * Store a newly created resource in storage. 
  28.      * 
  29.      * @param  \Illuminate\Http\Reques $request 
  30.      * @return \Illuminate\Http\Response 
  31.      */  
  32. public function store(Request $request)  
  33. {  
  34.   
  35.         //  
  36.  }  
  37.   
  38.   /** 
  39.      * Display the specified resource. 
  40.      * 
  41.      * @param  int  $id 
  42.      * @return \Illuminate\Http\Response 
  43.      */  
  44.       
  45. public function show($id)  
  46.       
  47. {  
  48.           
  49. //  
  50.       
  51. }  
  52.   
  53.   /** 
  54.      * Show the form for editing the specified resource. 
  55.      * 
  56.      * @param  int  $id 
  57.      * @return   
  58. \Illuminate\Http\Response 
  59.      */  
  60.       
  61. public function edit($id)  
  62.       
  63. {  
  64.           
  65. //  
  66.       
  67. }  
  68.   
  69.    /** 
  70.      * Update the specified resource in storage. 
  71.      * 
  72.      * @param  \Illuminate\Http\Request    
  73. $request 
  74.      * @param  int  $id 
  75.      * @return \Illuminate\Http\Response 
  76.      */  
  77.       
  78. public function update(Request $request, $id)  
  79.       
  80. {  
  81.           
  82. //  
  83.       
  84. }  
  85.   
  86.       
  87. /** 
  88.      * Remove the specified resource from storage. 
  89.      * 
  90.      * @param  int  $id 
  91.      * @return   
  92. \Illuminate\Http\Response 
  93.      */  
  94.       
  95. public function destroy($id)  
  96.      
  97.  {  
  98.           
  99. //  
  100.       
  101. }  
  102. }  

The above code contains the functions which are used to perform the various operations on the resources such as:

create(): It is used to create a new resource.

store(): It is used to store the specified resource.

update(): It is used to update the specified resource in the storage.

destroy(): It is used to remove the specified resources from the storage.