web/routes.php file में closure function के through Request handle करने की वजाय आप उस Request को Controller में handle कर सकते हैं। Controllers का use करके हम एक particular module के लिए सभी actions को group कर सकते हैं , जिससे उस module को handle करने में आसानी रहेगी।


एक Posts module के लिए सभी mandatory actions like : list , create , store , edit, update , delete etc. के लिए आपको अलग - अलग Files बनाने की जरूरत नहीं है , simply एक PostController बनाकर सभी actions use Controller में handle करेंगे। जिससे हर module की एक अलग file बन जायगी और उन्हें manage करना भी easy रहेगा।

Defining Controller

Laravel Application में सभी Controllers को app/Http/Controllers directory में रखा जाता है। अब एक TestController create करेंगे जिसके एक हम simple "Hello Laravel" print कराने की koshish करेंगे।


Controller create करने के लिए आप php artisan command भी run कर सकते हैं , या app/Http/Controllers directory में जाकर manually भी create कर सकते हैं।

php artisan make:controller TestController

File : app/Http/Controllers/TestController.php

Copy Fullscreen Close Fullscreen
<?php 
  namespace App\Http\Controllers;
  class TestController extends Controller
  {
    /**
     * Show simple output.
     *
     * @param  int  $id
     * @return \Illuminate\View\View
     */
    public function index()
    {
        return "Hello Laravel";
    }
 }

अब routes/web.php Open करें एक route create करके इस Controller को point करें।

Route::get('/test', 'TestController@index');

That's it. अब अपने browser open करके url locate करें - http://you-app.com/test

Note : Controller use करने से पहले आप app/Providers/RouteServiceProvider.php file Open करके ये check कर लें कि , web routes के लिए Controllers के लिए default path क्या है, अगर path app/Http/Controllers नहीं है तो इसे edit कर दें। नहीं तो route में define किया गया Controller point नहीं हो पाएगा।

Passing Parameters

हालाँकि अगर आप Parameter handle करना चाहते हैं , तो routes में defined parameter के according उस method में Parameters define दीजिये।

Fullscreen Close Fullscreen
/*required parameter : routes/web.php*/
Route::get('/test/{name}', 'TestController@index');

/*Acces it in TestController@index method*/
public function index($name)
{
   return "Hello ". $name;
}

/*For Optional Parameter : routes/web.php*/
Route::get('/test/{f_name}/{l_name?}', 'TestController@index');
/*Acces it in TestController@index method*/
public function index($f_name, $l_name=null)
{
   return "Hello ". $f_name. " ".$l_name;
}

Laravel Single Action Controllers

अगर किसी Controller में सिर्फ एक single action perform करना चाहते हैं तो उस Controller में __invoke() method define करके यह काम कर सकते हैं।

File : app/Http/Controllers/TestController.php

Copy Fullscreen Close Fullscreen
<?php 
  namespace App\Http\Controllers;
  class TestController extends Controller
  {
    public function __invoke()
    {
        return "Hello Laravel";
    }
 }

/*define route : routes/web.php*/
Route::post('/server', TestController::class);

कुछ इस तरह से single action perform करने के लिए __invoke() method बनाते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers