我目前正在使用Laravel 4遇到麻烦。我想将slugs用于论坛类别和论坛主题(slugs是独一无二的)。为了确定用户是在类别中还是在主题中,我有这条路线:
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';
}
});
当我尝试达到某个类别时出现以下错误:
Route [ForumCategoryController@findBySlug] not defined.
这是我的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
));
}
}
问题出在哪里?有没有办法做得更好?请帮助:)
答案 0 :(得分:4)
Laravel告诉你必须定义一条使用Route::action()
的路线,例如:
Route::get('forum/bySlug/{slug}', 'ForumTopicController@findBySlug');
因为它实际上会建立一个网址并完善它:
http://your-box/forum/bySlug/{slug}
为此,它必须找到指向你行动的路线。