问题
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