Question

I am sure that I am doing something wrong that is very obvious, but for some reason I cannot get any filters except App::before to work in my test application.

//routes.php

Route::get('site/login',
               array(
                    'before'=>'science',
                    'as'=>'site/login',
                    'uses'=>'HomeController@getLogin',
               )
           );
Route::controller(site, 'HomeController');

//filters.php

App::before(function($request){
    //var_dump("Before"); exit;
});

Route::filter('science',function(){
   dd("Science B!TCH!");
   exit;
});

//HomeController.php

public function getLogin(){
    $this->layout->body = View::make('home.login');
}

The object was first to ensure that a user was not logged in so I was trying to use the built-in "guest" filter, but it was never being called. So I later created the "science" filter to test if ANY routes would work. If I uncomment the var_dump line in App::before, it displays "Before" and exits as expected.

Can anyone see what I am doing wrong here? When I go to the /site/login page I should see my Breaking Bad movie quote instead of the actual page. However, I am seeing my login form as if nothing was happening.

Thanks!

UPDATE: I changed the route to look like this now:

//routes.php

Route::get('site/login', 'HomeController@getLogin')->before('science');

... and it works. I get the debugging string "SCIENCE ..." on the screen.

It also works if I do the following

//HomeController.php

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

Are there any use cases or conditions in which the array version of routes gets ignored?

UPDATE 2: In my efforts to simplify my original description I neglected to show other routes that were in routes.php. Take a look below.

//routes.php

Route::get('site/login',
                    array(
                        'before'=>'science',
                        'as'=>'site/login',
                        'uses'=>'HomeController@getLogin'
                    )
                );
Route::post('site/login',
               array(
                    'as'=>'site/login',
                    'uses'=>'HomeController@postLogin'
               )
);

Having the POST route AFTER the GET route is what is causing the problem. When I put the POST route BEFORE the GET route, the GET route works with the filter as expected.

Now, I was under the impression that Laravel treated GET and POST requests differently, hence the usage of the different static methods in Route. However, apparently, this is not true as the filter on the latter affects the filter of the former.

Is this a correct assumption? Should I start a different thread about this? I would love to understand why this is working this way.

Thanks!

UPDATE 3

---- SOLVED ---

This tidbit of information is not specifically stated in the documentation but you cannot have identical route names even though those route names are going to different REST verbs.

//routes.php BEFORE

Route::get('site/login',  array('as'=>'site/login','uses'=>'HomeController@getLogin', 'before'=>'science'));
Route::post('site/login', array('as'=>'site/login', 'uses'=>'HomeController@postLogin',));

In the above solution, the 2nd Route OVERRIDES the previous route because the "as" uses the same name. I thought that these would be treated differently since one is GET and the other POST, but this is not the case. The filter assignments must happen by name in the backend and, as such, using identical names will override each other.

//routes.php AFTER

Route::get('site/login',  array('as'=>'site/login','uses'=>'HomeController@getLogin', 'before'=>'science'));
Route::post('site/login', array('as'=>'site/postLogin', 'uses'=>'HomeController@postLogin',));

As you can see here, I renamed the 'as' part of the array to 'site/postLogin' and I can now use different filters for each the POST, GET, and probably PUT, DELETE and etc.

Was it helpful?

Solution 2

---- SOLVED ---

This tidbit of information is not specifically stated in the documentation but you cannot have identical route names even though those route names are going to different REST verbs.

//routes.php BEFORE

Route::get('site/login',  array('as'=>'site/login','uses'=>'HomeController@getLogin', 'before'=>'science'));
Route::post('site/login', array('as'=>'site/login', 'uses'=>'HomeController@postLogin',));

In the above solution, the 2nd Route OVERRIDES the previous route because the "as" uses the same name. I thought that these would be treated differently since one is GET and the other POST, but this is not the case. The filter assignments must happen by name in the backend and, as such, using identical names will override each other.

//routes.php AFTER

Route::get('site/login',  array('as'=>'site/login','uses'=>'HomeController@getLogin', 'before'=>'science'));
Route::post('site/login', array('as'=>'site/postLogin', 'uses'=>'HomeController@postLogin',));

As you can see here, I renamed the 'as' part of the array to 'site/postLogin' and I can now use different filters for each the POST, GET, and probably PUT, DELETE and etc.

OTHER TIPS

For better practice if two or more routes use the same filter, those routes should belong in a group. I have a feeling that will correct the issue.

From http://laravel.com/docs/routing#route-groups

Route::group(array('before' => 'auth'), function()
{
    Route::get('/', function()
    {
        // Has Auth Filter
    });

    Route::get('user/profile', function()
    {
        // Has Auth Filter
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top