Laravel Application के लिए सभी route , routes directory में defined होते हैं। अगर Laravel Version 5.4 या इससे ज्यादा है तो सभी route, routes Directory के अंदर मिलेंगे। और अगर Laravel Version 5.3 या इससे काम है तो सभी route, app/Http/routes.php या app/routes.php में मिलते हैं।

routes Directory में आप aaplication के सभी तरह के routes (web routes , api routes और console routes) रखते हैं।

Laravel Basic Routing

Laravel में सबसे Basic route एक URL और Closure को accept करता है। जिसे कुछ इस तरह से लिखते हैं।

File Location : routes > web.php

Route::get('hello', function () {
    return 'Hello Laravel';
});

यह एक GET Type की request के लिए routes है , Browser में अपने project name के बाद /hello लिखेंगे तो आपको "Hello Laravel" मिलेगा।

Laravel Default Route Files

web.php file defined सभी routes को directly आप http://your-app/url से access कर सकते हैं , जो कि web middleware group को assigned होती हैं जो session state and CSRF protection जैसे features provide करती है।

API routes के लिए आप सभी routes को routes/api.php में register कर सकते हैं। api routes stateless होते हैं , और ये routes api middleware group को assigned होते हैं।


api routes के लिए /api automatically prefix होता है , जिसे आप app/Providers/RouteServiceProvider.php में जाकर customize कर सकते हैं।

Laravel Routing Methods

Route::get('url', callback);
Route::post('url', callback);
Route::match(['get', 'post'], 'url', callback);
Route::put('url', callback);
Route::patch('url', callback);
Route::delete('url', callback);
Route::any('url', callback);

हालाँकि HTML Forms PUT, PATCH, or DELETE type की request नहीं बन सकते हैं , इसलिए हमें manually एक _method name का hidden field या @method blade directive का use करना पड़ता है।

<form action="/action/abcd" method="POST">
    @method('PUT')
     or
    <input type="hidden" value="PUT" name="_method">
</form>


Laravel CSRF Protection

कोई भी HTML Form जो POST, PUT, PATCH, or DELETE type की request को point कर रहा हो और जो web routes में defined हो , उस request के लिए एक CSRF token field add कर लेना चाहिए , otherwise request reject हो जायगी।
CSRF token field add करने के लिए simply <form></form> tag आप @csrf लिखें या manually hidden input field लिखें।

For Example :

<form action="/action/abcd" method="POST">
    @csrf
     or
    <input type="hidden" value="{{csrf_token()}}" name="_token">
</form>

Laravel Route Parameters

Required Parameter

कभी - कभी आपके URL में से Parameters को capture करने की जरूरत पड़ जाती है , but उसके लिए हमें उस Parameter को route के साथ define करना पड़ता है।

Route::get('user/{id}', function($id){
   return "User Id : ". $id;
});

हालाँकि आप अपनी need according कितने ही parameters define कर सकते है।

Route::get('post/{id}/comments/{comment}', function(){
   //do whatever you want
});

Route Optional Parameters

हालाँकि आप , Optional Parameter भी define कर सकते हैं। Optional Parameter में आप route में parameter pass भी कर सकते हैं और नहीं भी।

Route::get('user/{first_name}/{last_name?}', function($first_name , $last_name=null){
   return "Welcome : ".$first_name.' '.$last_name;
});

Example में last_name Optional Parameter है।

Use Controller To Handle Request

अभी तक हम Closure Function के through request या request Parameter को Handle कर रहे थे , Request को Controller में Handle करने के लिए हमें Controller का name और method का name define करना पड़ता है।

Route::get('user', 'UserController@user_inde');
/*make Your Controller , app/Http/Controllers/UserController.php  */

Controller के किसी particular method को hit करने के लिए , @ के बाद method का name लिखते हैं।


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

Laravel Named Routes

Laravel में आप किसी URL को एक unique name भी दे सकते हैं , और उस name का use करके URLs generate कर सकते हो और redirects भी। लेकिन name के thorugh URL generate करने के लिए हमें route() function का use करना पड़ता है।

Route::get('user/profile', function () {
    //your logic
})->name('user-profile');

आप Controller के लिए भी named route define कर सकते हैं। ये आप दो तरह से define कर सकते हैं।

Route::get('user/profile', 'UserProfileController@profile')->name('user-profile');
Or
Route::get('user/profile', ['as' => 'user-profile', 'uses' => 'UserProfileController@profile']);

Generating URLs To Named Routes

URL generate या redirect करने के लिए route helper function का use करते हैं।

$user_profile_url = route('user-profile');
/*to redirect from location to other*/
return redirect( route('user-profile') );
or
return redirect()->route('user-profile');

/*if you want to pass parameter*/
$user_profile_url = route('user-profile', 'param1');
or 
$user_profile_url = route('user-profile', ['param1', 'param2']);

Laravel Route Prefixes

कभी कभी हमें हर URL में एक particular जगह पर same URL segment add करना होता है , उसके लिए हम prefix() method use करते हैं।

Route::prefix('user')->group(function () {
    Route::get('/', function () {
        // Matches The "www.yourapp.com/user" URL
    });
    Route::get('/profile', function () {
        // Matches The "www.yourapp.com/user/profile" URL
    });
});

या directly group method का use करके भी , directly prefix और middleware  define कर सकते हैं।

Route::group(['prefix' => 'user'], function () {
    Route::get('/', function () {
        // Matches The "www.yourapp.com/user" URL
    });
    Route::get('/profile', function () {
        // Matches The "www.yourapp.com/user/profile" URL
    });
});

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