2013-07-03 8 views
8

L'ho aggiunto in routes.php, atteso che verificherà la sessione di autenticazione per la pagina, tuttavia non funziona.Le risorse di percorso impostate prima dell'autorizzazione non funzionano in Laravel 4

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

Poi vado al controller, lavoro in un altro modo. È un lavoro.

Posso sapere dove posso ottenere più documentazione riguardo a Route :: resource(), che tipo di argomento è in grado di accettare?

risposta

21

OK ... Ho trovato la risposta.

in

\ vendor \ laravel \ Framework \ src \ Illuminate \ Routing \ 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; 
    } 

come si può vedere, si accettano solo solo only e except arguement.

Se si desidera archiviare lo stesso risultato in route.php, si può fare, come di seguito

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

Oppure si potrebbe usare del controller beforeFilter metodo(). '$ this-> beforeFilter ('auth', ['except' => 'destroy']);'. Controlla il commento di Devon a [questo link] (https://laracasts.com/index.php/discuss/channels/general-discussion/how-can-i-declare-a-before-filter-on-a-routeresource) –

Problemi correlati