在路由中使用资源时在控制器中使用自定义函数 - Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28877097/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:04:08  来源:igfitidea点击:

Use custom function in Controller when using resource in routes - Laravel

phplaravelmodel-view-controllerlaravel-4routes

提问by Lovelock

Been building a framework website using Laravel and working on the user system.

一直在使用 Laravel 构建框架网站并在用户系统上工作。

I am using the controller by a resource route:

我通过资源路由使用控制器:

Route::resource('user', 'UserController');

Which works fine for all the normal create, index, store etc function in the controller.

这适用于控制器中的所有正常创建、索引、存储等功能。

For my registration form this is the opening:

对于我的注册表,这是开头:

{{ Form::open(array('route' => 'user.store', 'class'=>'small-form form-holder')) }}

Thinking how nice this is, I created a login function in my UserController and tried this for my login form:

想想这有多好,我在我的 UserController 中创建了一个登录功能,并在我的登录表单中尝试了这个:

{{ Form::open(array('route' => 'user.login', 'class'=>'small-form form-holder')) }}

However this returns a route not defined error. Is this because of the resource route that I set? I know I could set a custom route which uses the controllers login method but I like this way of doing things.

但是,这会返回路由未定义错误。这是因为我设置的资源路由吗?我知道我可以设置一个使用控制器登录方法的自定义路由,但我喜欢这种做事方式。

回答by mininoz

Using Resource will generate following route names by default

使用 Resource 默认会生成以下路由名称

user.index
user.create
user.store
user.show
user.edit
user.update
user.destroy

more information RESTful Resource Controllers

更多信息RESTful 资源控制器

If you want to create another route you could do this way

如果你想创建另一条路线,你可以这样做

Route::get('user/login', 'UserController@login');

Route::resource('user', 'UserController');

note: you should define those routes before your call to Route::resource

注意:您应该在调用 Route::resource 之前定义这些路由

for more information you can look at RESTful Resource Controllersin Adding Additional Routes To Resource Controllerssession

有关更多信息,您可以在资源控制器添加附加路由会话中查看RESTful 资源控制器