laravel 表单对控制器方法开放 - “未知操作”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16879797/
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
Form open to controller method - "Unknown action"
提问by user2444492
New to Laravel 4. I've created a form within a blade template and I'm following the snippet fromwhich says that you can point a forms action to a controller method by using 'Form::open(array('action' => 'Controller@method'))'. I've created a new controller called UsersController with artisan and have created a new method within the controller named userLogin(). When I point to that method when opening a form I get an "InvalidArgumentException, Unknown action" error. If I adjust the open action to point to UsersController@index, all is well. I've run composer dump-autoload, but the issue remains.
Laravel 4 的新手。我已经在刀片模板中创建了一个表单,我正在关注其中的片段,其中说您可以使用 'Form::open(array('action' = > '控制器@方法'))'。我用 artisan 创建了一个名为 UsersController 的新控制器,并在名为 userLogin() 的控制器中创建了一个新方法。当我在打开表单时指向该方法时,我收到“InvalidArgumentException, Unknown action”错误。如果我将打开操作调整为指向 UsersController@index,一切都很好。我已经运行 composer dump-autoload,但问题仍然存在。
snippet of login.blade.php:
login.blade.php 的片段:
{{ Form::open(array('action' => 'UsersController@userLogin')) }}
snippet of UsersController.php:
UsersController.php 的片段:
public function userLogin()
{
//
}
Can anyone tell me if I'm missing something?
谁能告诉我我是否遗漏了什么?
Thanks all. Adding the following to routes.php resolved the issue:
谢谢大家。将以下内容添加到 routes.php 解决了问题:
Route::post('login', 'UsersController@userLogin');
回答by zeantsoi
Looks like Laravel isn't registering the action you've added, likely because you're missing a route. Try adding something like this to app/routes.php
:
看起来 Laravel 没有注册您添加的操作,可能是因为您缺少路线。尝试将这样的内容添加到app/routes.php
:
Route::post('user/login', 'UsersController@userLogin');
回答by ollieread
After adding the route to your routes.php, did you also change Form::open()? If not, you can just have your Form post to /login or /user/login.
将路由添加到 routes.php 后,您是否还更改了 Form::open()?如果没有,您可以将表单发布到 /login 或 /user/login。
Also, just because I'm a bit of a stickler for these sort of things, it's common practise to have controllers and models as singular, so UsersController would be UserController, and since the login function is within User(s)Controller, it doesn't need the user prefix. May help your code be more readable :)
此外,仅仅因为我对这些事情有点坚持,通常的做法是将控制器和模型设为单数,因此 UsersController 将是 UserController,并且由于登录功能在 User(s)Controller 中,因此它不会不需要用户前缀。可能会帮助您的代码更具可读性 :)
回答by George Abitbol
Now in laravel 4 you can use this :
现在在 laravel 4 中你可以使用这个:
Route::post('/signup', array('before' => 'csrf', 'uses' => 'UsersController@userLogin'));