Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route resources set before auth not working in Laravel 4

I added this in routes.php, expected it will check the authentication session for the page, however it is not working.

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

Then I go to the controller, work in another way. It's work.

class TicketController extends BaseController {

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

May I know where can get more documentation regarding the Route::resource(), what type of argument it able to accept?

like image 548
Shiro Avatar asked Jul 03 '13 02:07

Shiro


1 Answers

OK... I found the answer.

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;
    }

as you can see, it only accept only and except arguement only.

If you want to archive the same result in route.php, it can be done as below

Route::group(array('before'=>'auth'), function() {   
    Route::resource('ticket', 'TicketController');
});
like image 81
Shiro Avatar answered Nov 03 '22 23:11

Shiro