user1578301
user1578301

Reputation: 85

Laravel 4 Custom Routing

Hello Everyone I'm new in laravel and i want to ask a question. This my local url

/laravel-master/public/

I have some routing I made.

Route::resource('news', 'NewsController');
Route::resource('events', 'EventsController');
Route::resource('products', 'ProductsController');

That's 3 routes working normally. But i don't want to repeat my self with a lot of menu so i made a change to the code:

Route::get('{class}', function($class){
    Route::controller($class,ucfirst($class).'Controller@index');
})->where('class', 
    'news|events|products|anotherclass');

and i got an error like this

ReflectionException Class NewsController@index does not exist

Actually, NewsController does exist and Index method does exist:

class NewsController extends BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        return View::make('page.news-event');
    }
}

And I want to handle url {pagename}-page with controller page and get the pagename parameter

Route::get('{pagename}-page', function($pagename){
    Route::controller($pagename.'-page','PagesController@index');// where i can put the parameter page name?
});



class PagesController extends BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index($pagename='')
    {
        if($pagename=='about'){
            // do something
        }else if($pagename=='contact'){
            // do something
        }
        return View::make('page.about');
    }
}

php artisan route:

|        | GET /{class}                                        |                                | Closure                                                        |                                                        |               |
|        | GET /{pagename}-page                                   |                                | Closure                                                        |                                                        |               |
|        | GET /                                               |                                | HomeController@index                                           |                                                        |               |

Please help. Sorry for my bad english.

Upvotes: 0

Views: 2458

Answers (2)

Rob Gordijn
Rob Gordijn

Reputation: 6511

You are defining an RESTful route in your closure with 'Classname@index'.

If you look at the manual: http://laravel.com/docs/controllers#restful-controllers You will see you only must specify the classname, no @ and method name.

And, I'm not sure if it will work in the way you are trying...

If you want to repeat urls for resources, make a foreach loop that contains the url and the class name. Register the Route::resource() within that loop.

Upvotes: 1

Anshad Vattapoyil
Anshad Vattapoyil

Reputation: 23463

May be the problem is with class auto-loading issue. So run,

composer dump-autoload

And also try to replace class from your routing with something else like slug

Route::get('{slug}', function($slug){
    Route::controller($slug,ucfirst($slug).'Controller@index');
})->where('slug', 'news|events|products|anotherclass');

Upvotes: 0

Related Questions