2013-07-03 38 views
8

我在routes.php中添加了此項,期望它將檢查頁面的身份驗證會話,但它不起作用。Auth未在Laravel工作之前設置的路由資源4

Route::resource('ticket', 'TicketController', array('before' => 'auth')); 

然後我去控制器,以另一種方式工作。是工作。

class TicketController extends BaseController { 

public function __construct() 
{ 
    $this->beforeFilter('auth'); 
} 

我可以從哪裏獲得更多有關Route :: resource()的文檔,它能夠接受哪種類型的參數?

回答

21

好的......我找到了答案。

\供應商\ laravel \框架的\ src \照亮\路由\ Router.php

public function resource($resource, $controller, array $options = array()) 
    { 
     // If the resource name contains a slash, we will assume the developer wishes to 
     // register these resource routes with a prefix so we will set that up out of 
     // the box so they don't have to mess with it. Otherwise, we will continue. 
     if (str_contains($resource, '/')) 
     { 
      $this->prefixedResource($resource, $controller, $options); 

      return; 
     } 

     // We need to extract the base resource from the resource name. Nested resources 
     // are supported in the framework, but we need to know what name to use for a 
     // place-holder on the route wildcards, which should be the base resources. 
     $base = $this->getBaseResource($resource); 

     $defaults = $this->resourceDefaults; 

     foreach ($this->getResourceMethods($defaults, $options) as $method) 
     { 
      $this->{'addResource'.ucfirst($method)}($resource, $base, $controller); 
     } 
    } 

protected function getResourceMethods($defaults, $options) 
    { 
     if (isset($options['only'])) 
     { 
      return array_intersect($defaults, $options['only']); 
     } 
     elseif (isset($options['except'])) 
     { 
      return array_diff($defaults, $options['except']); 
     } 

     return $defaults; 
    } 

,你可以看到,它僅僅只接受onlyexcept arguement。

如果要存檔在route.php同樣的結果,這是可以做到如下

Route::group(array('before'=>'auth'), function() { 
    Route::resource('ticket', 'TicketController'); 
}); 
+0

或者你可以使用控制器的beforeFilter()方法。 '$ this-> beforeFilter('auth',['except'=>'destroy']);'。在[此鏈接]查看Devon的評論(https://laracasts.com/index.php/discuss/channels/general-discussion/how-can-i-declare-a-before-filter-on-a-routeresource) –