2013-07-03 11 views
8

routes.phpにこれを追加しました。ページの認証セッションをチェックすると予想されますが、動作しません。ルートリソースがLaravel 4で動作しない前に設定されています

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

次に、別の方法で作業します。それは仕事です。受け入れること

class TicketController extends BaseController { 

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

ルートに関するより多くのドキュメントを取得することができる場所私が知っているかもしれません::リソース()は、引数の種類それができますか?

答えて

21

OK ...私は答えを見つけました。

あなたが見ることができるように、それだけでのみonlyexcept arguementを受け入れる\ベンダー\ 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; 
    } 

インチ

あなたはroute.phpで同じ結果をアーカイブする場合、それは

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

次のように行うことができます。また、コントローラのbeforeFilterの()メソッドを使用することができます。 '$ this-> beforeFilter( 'auth'、['except' => 'destroy']))'。デボンのコメントを[このリンク]で確認してください(https://laracasts.com/index.php/discuss/channels/general-discussion/how-can-i-declare-a-before-filter-on-a-routeresource) –

関連する問題