Aristona
Aristona

Reputation: 9331

Prioritizing Laravel 4 routes

I have routes like this:

Route::group(array('before' => 'installed'), function() {

    Route::group(array('before' => 'auth_admin', 'prefix' => 'admin'), function()
    {

        Route::group(array('prefix' => 'gag'), function() {

            Route::get('/', 'Admin\\GagController@index');
            Route::get('delete/{id}','Admin\\GagController@delete');

        });

    });

});

I need to prevent users from deleting content on my demo application. So I added the following piece of code before my actual routes.

if(App::environment() === 'demo')
{
     Route::get('admin/gag/delete/{id}', function() {
          die("You can't delete anything on demo application.");
     });
}

//Actual routes are at the below.

However, it doesn't work when Route::get('delete/{id}','Admin\\GagController@delete'); is there. Somehow, Laravel ignores my if block and priorities this route. (Although if block is at the top.)

Looks like routes.php parses my if block after routes are parsed.

How can I make it so Laravel prioritizes my demo routes? I just want to restrict access to some features like this.

Ps. I don't want to add all the routes in if blocks. I just want the routes in my if block to be prioritized.

Upvotes: 1

Views: 445

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

Route filters are better to do this sort of restrictions:

Route::filter('checkDemo', function()
{
    if (App::environment() === 'demo')
    {
        return Redirect::to('home')->withMessage('You can''t delete anything on demo application.');
    }
});

And set the filter to your route:

Route::group(array('prefix' => 'gag', 'before' => 'checkDemo'), function() 
{
  ...
});

Or you can filter just that particular route:

Route::get('delete/{id}', array('before' => 'checkDemo', 'uses' => 'Admin\\GagController@delete'));

Upvotes: 2

Related Questions