Laravel 4 Redirect::action() “Route not defined”

ⅰ亾dé卋堺 提交于 2019-12-10 15:54:55

问题


I'm currently having troubles with Laravel 4. I would like to use slugs for forum categories and forum topics (slugs are unique). In order to determinate if the user is in a category or in a topic, I have this route:

Route::get('forum/{slug}', function($slug) {

    $category = ForumCategory::where('slug', '=', $slug)->first();

    if (!is_null($category))
        return Redirect::action('ForumCategoryController@findBySlug', array('slug' => $slug));

    else {

        $topic = ForumTopic::where('slug', '=', $slug)->first();

        if (!is_null($topic))
            return Redirect::action('ForumTopicController@findBySlug', array('slug' => $slug));

        else
            return 'fail';

    }

});

And I have the following error when I try to reach a category:

Route [ForumCategoryController@findBySlug] not defined.

Here is my ForumCategoryController:

class ForumCategoryController extends BaseController {

    public function findBySlug($slug) {

        $category = ForumCategory::where('slug', '=', $slug)->first();

        return View::make('forum.category', array(
            'title'         => 'Catégorie',
            'category'      => $category
        ));

    }

}

Where is the problem ? Is there a way to do it better ? Help please :)


回答1:


Laravel is telling that you have to define a route to use Route::action(), something like:

Route::get('forum/bySlug/{slug}', 'ForumTopicController@findBySlug');

Because it will actually build an url and consumme it:

http://your-box/forum/bySlug/{slug}

For that it must find a route pointing to your action.



来源:https://stackoverflow.com/questions/21416083/laravel-4-redirectaction-route-not-defined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!