2013-08-15 7 views
22

क्या फ़िल्टर के भीतर रूट पैरामीटर तक पहुंच बनाना संभव है?फ़िल्टर के लिए तर्क पास करना - लार्वेल 4

उदा।

Route::group(array('prefix' => 'agency'), function() 
{ 

    # Agency Dashboard 
    Route::get('{agencyId}', array('as' => 'agency', 'uses' => 'Controllers\Agency\[email protected]')); 

}); 

मैं अपने फिल्टर के भीतर इस $ agencyId पैरामीटर का उपयोग करना चाहते हैं:: मैं $ agencyId पैरामीटर उपयोग करना चाहते

Route::filter('agency-auth', function() 
{ 
    // Check if the user is logged in 
    if (! Sentry::check()) 
    { 
     // Store the current uri in the session 
     Session::put('loginRedirect', Request::url()); 

     // Redirect to the login page 
     return Redirect::route('signin'); 
    } 

    // this clearly does not work..? how do i do this? 
    $agencyId = Input::get('agencyId'); 

    $agency = Sentry::getGroupProvider()->findById($agencyId); 

    // Check if the user has access to the admin page 
    if (! Sentry::getUser()->inGroup($agency)) 
    { 
     // Show the insufficient permissions page 
     return App::abort(403); 
    } 
}); 

बस संदर्भ के लिए मैं इस तरह के रूप में अपने नियंत्रक में इस फिल्टर फोन:

class AgencyController extends AuthorizedController { 

    /** 
    * Initializer. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     // Apply the admin auth filter 
     $this->beforeFilter('agency-auth'); 
    } 
... 
+2

आप इस '$ का उपयोग कर सकते हैं:

$this->beforeFilter(function($route, $request) { $userId = $route->parameter('users'); }); 

एक अन्य विकल्प Route मुखौटा है, जो उपयोगी है जब आप एक मार्ग के बाहर हैं के माध्यम से पैरामीटर को पुनः प्राप्त करने के लिए है एजेंसीआईडी ​​= अनुरोध :: सेगमेंट (2) 'फ़िल्टर में 'एजेंसी आईडी' प्राप्त करने के लिए –

उत्तर

28

Input::get केवल GET या POST (और इसी तरह) तर्कों को पुनर्प्राप्त कर सकता है।

मार्ग मानकों पाने के लिए आपको, आपके फ़िल्टर में Route वस्तु हड़पने के लिए है इस तरह:

Route::filter('agency-auth', function($route) { ... }); 

और मानकों (आपके फ़िल्टर में) मिलता है:

$route->getParameter('agencyId'); 

(बस मस्ती के लिए) आपके मार्ग में

Route::get('{agencyId}', array('as' => 'agency', 'uses' => 'Controllers\Agency\[email protected]')); 

आप अपने कन्स्ट्रक्टर में विस्तार करने के बजाय पैरामीटर सरणी 'before' => 'YOUR_FILTER' में उपयोग कर सकते हैं।

14

लार्वेल 4.1 से parameter में विधि का नाम बदल गया है। उदाहरण के लिए, एक RESTful नियंत्रक में:

$id = Route::input('id'); 
संबंधित मुद्दे