Laravel 4 路由到控制器方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21103650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 routing to controller method
提问by ranisalt
I'm currently trying to route as follows:
我目前正在尝试路由如下:
- If user GETs
/account/
- If session has
account_id
, user is logged in; show his account information - If not, user is not logged in; show login/create form
- If session has
- If user POSTs
/account/
- If input has
create
, user wants to create account; create it - If not, user wants to login; find his account and go again to
/account/
- If input has
- 如果用户获取
/account/
- 如果 session 有
account_id
,则用户已登录;显示他的账户信息 - 如果不是,则用户未登录;显示登录/创建表单
- 如果 session 有
- 如果用户 POST
/account/
- 如果输入有
create
,则用户要创建帐户;创造它 - 如果不是,用户想登录;找到他的帐户,然后再去
/account/
- 如果输入有
My routes are set this way:
我的路线是这样设置的:
Route::get('account', function() {
if (Session::has('account_id'))
return 'AccountsController@show';
else
return 'AccountsController@index';
});
Route::post('account', function() {
if (Input::has('create')) {
return 'AccountsController@create';
else
return 'AccountsController@login';
)};
This is somewhat how I would do with Rails, but I don't know how to point to the controller method. I just get the returned string as a result. I didn't find it in Laravel documentation (which I found really poor, or have I searched wrong?) neither in any other web tutorial.
这在某种程度上是我对 Rails 的处理方式,但我不知道如何指向控制器方法。结果我只是得到了返回的字符串。我没有在 Laravel 文档中找到它(我发现它真的很糟糕,或者我搜索错了?)也没有在任何其他网络教程中找到。
回答by Anam
Try the following:
请尝试以下操作:
Route::get('account', function() {
if (Session::has('account_id')) {
$action = 'show';
return App::make('AccountsController')->$action();
}
else {
$action = 'index';
return App::make('AccountsController')->$action();
}
});
Route::post('account', function() {
if (Input::has('create')) {
$action = 'create';
return App::make('AccountsController')->$action();
}
else {
$action = 'login';
return App::make('AccountsController')->$action();
}
)};
回答by elliotanderson
So, you want to put all your logic in the controllers.
所以,你想把所有的逻辑都放在控制器中。
You would want to do
你会想要做
Route::get('account', 'AccountsController@someFunction');
Route::get('account', 'AccountsController@anotherFunction');
Then, in the respective controllers, you would want to make your tests, then do, for example,
然后,在相应的控制器中,您需要进行测试,然后执行,例如,
if (Session::has('account_id')){
return Redirect::action('AccountsController@show');
}
else{
return Redirect::action('AccountsController@index');
}
However, make sure you define a route for each action, so your application can detect a URL.
For example, if you want to have
但是,请确保为每个操作定义一个路由,以便您的应用程序可以检测到一个 URL。
例如,如果你想拥有
Redirect::action('AccountsController@show')
you will need to define this action like:
您将需要定义此操作,例如:
Route::get('/account/show', 'AccountsController@show')