Laravel 5单路径多控制器方法 [英] Laravel 5 single route multiple controller method

查看:50
本文介绍了Laravel 5单路径多控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条带参数的路线

Route::get('forum/{ques}', "ForumQuestionsController@show");

现在我想要一条类似的路线

Now I want a route something like

Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController@add"]);

好吧,当我按下localhost:800/forum/add时,我被路由到ForumQuestionsController@show而不是ForumQuestionsController@add

well when I hit localhost:800/forum/add I get routed to ForumQuestionsController@show instead of ForumQuestionsController@add

我知道我可以在ForumQuestionsController的show方法中处理此问题,并根据参数返回不同的视图.但是我想要这种方式.

Well I know I can handle this in show method of ForumQuestionsController and return a different view based on the paramter. But I want it in this way.

推荐答案

首先给这个

Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController@add"]);

然后以下

Route::get('forum/{ques}', "ForumQuestionsController@show");

另一种方法(使用正则表达式约束)

Another Method (using Regular Expression Constraints)

Route::pattern('ques', '[0-9]+');
Route::get('forum/{ques}', "ForumQuestionsController@show");

如果ques是一个数字,它将自动转到show方法,否则添加方法

If ques is a number it will automatically go to the show method, otherwise add method

这篇关于Laravel 5单路径多控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆