user3189734
user3189734

Reputation: 665

Laravel 4 - Setting up routes

I'm working on a site I have inherited and having a little trouble routing to a controller.

When I visit the URL www.domain.com/banners/statistics, it won't return anything.

I also noted that when I try and link to this page via Banner Statistics this also gives me an error on my home page.

Routes.php

Route::resource('banners', 'BannerController');
Route::get('banners/{banners}/activate', 'BannerController@activate');
Route::get('banners/{banners}/deactivate', 'BannerController@deactivate');
Route::get('banners/{banners}/delete', 'BannerController@delete');
Route::get('banners/{banners}/preview', 'BannerController@preview');
Route::any('banners/{banners}/cropresize', 'BannerController@cropresize');
Route::get('banners/statistics', 'BannerController@statistics');

BannerController.php

public function create()
    {
        $data['title'] = 'Create Banner';
        $data['disciplines'] = Discipline::lists('name', 'id');
        return View::make('admin.banners.create', $data);
    }

    public function statistics()
    {
      return View::make('admin.banners.statistics');
    }

Upvotes: 0

Views: 100

Answers (1)

Needpoule
Needpoule

Reputation: 4576

The resource controller provides you multiple routes.

Including :

GET /resource/{resource} redirecting to the show action of your controller.

List of all created routes : http://laravel.com/docs/controllers#resource-controllers

So when you call

banners/statistics

Laravel think you want to call the show action with "statistics" as a parameter.

To avoid this, you can put all your custom routes above your resource controller route.

Route::get('banners/{banners}/activate', 'BannerController@activate');
Route::get('banners/{banners}/deactivate', 'BannerController@deactivate');
Route::get('banners/{banners}/delete', 'BannerController@delete');
Route::get('banners/{banners}/preview', 'BannerController@preview');
Route::any('banners/{banners}/cropresize', 'BannerController@cropresize');
Route::get('banners/statistics', 'BannerController@statistics');
Route::resource('banners', 'BannerController');

This way Laravel will call your custom route before the routes created by your resource controller.

You can also use only and except if you don't need some of the resource controller routes.

Route::resource('banners', 'BannerController',
                array('except' => array('show')));

Upvotes: 2

Related Questions