Laravel 5 中的登录和注册表单

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

Login and registration form in Laravel 5

laravellaravel-routinglaravel-5laravel-formlaravel-request

提问by mrakodol

I start to learn a new laravel and trying to learn it, by building small project, to make my starter site, but I have a problem. I only create new project without any plugin. I have a problem with login and register form. I put this into route file:

我开始学习一个新的 Laravel 并尝试通过构建小项目来学习它来制作我的入门网站,但我遇到了问题。我只创建没有任何插件的新项目。我在登录和注册表单方面遇到问题。我把它放到路由文件中:

get('user/login', 'Auth\AuthController@showLoginForm');
post('user/login', 'Auth\AuthController@postLogin');
get('user/register', 'Auth\AuthController@showRegistrationForm');
post('user/register', 'Auth\AuthController@register');

And make a login and register form. It shows me a forms, but I can't submit it and I do not know how to insert into database or how to validate user credentials. Can you tell me how to do it in new beta Laravel framework?

并制作登录和注册表格。它向我展示了一个表单,但我无法提交它,我不知道如何插入数据库或如何验证用户凭据。你能告诉我如何在新的 beta Laravel 框架中做到这一点吗?

Problem is that Laravel shows me a login and register form. But when I click submit it didn't do nothing, only refresh the page. I put method="POST" action="" into forms. When I change

问题是 Laravel 向我展示了一个登录和注册表单。但是当我点击提交时它什么也没做,只是刷新页面。我将 method="POST" action="" 放入表单中。当我改变

public function register(RegisterRequest $request)

into

进入

public function register()

in App\Http\Controllers\Auth\AuthController, when I submit form it give me an error that $request is not find.

在 App\Http\Controllers\Auth\AuthController 中,当我提交表单时,它给我一个错误,即找不到 $request。

You can see all code in: this link

您可以在以下位置查看所有代码: 此链接

采纳答案by Marcin Nabia?ek

This is how it works in Laravel 5 when you use request. If you inject Request object for example this way:

当您使用请求时,这就是它在 Laravel 5 中的工作方式。例如,如果您以这种方式注入 Request 对象:

public function register(RegisterRequest $request) {
   // your code goes here
}

the code of this function won't be executed until RegisterRequestvalidate your data with success.

RegisterRequest成功验证您的数据之前,不会执行此函数的代码。

Looking at your rules:

看看你的规则:

    return [
        'email' => 'required|email|unique:users',
        'password' => 'required|confirmed|min:8',
   ];

you need to make sure your data pass those rules.

您需要确保您的数据通过这些规则。

You should also add to your form displaying errors:

您还应该添加到显示错误的表单中:

@foreach ($errors->all() as $error)
 {{ $error }}
@endforeach

to display validation errors in form.

在表单中显示验证错误。

EDIT

编辑

Everything seems to be fine in your code. Valid function is being launched. If you don't believe me, change your registerfunction into:

在您的代码中一切似乎都很好。正在启动有效功能。如果您不相信我,请将您的register功能更改为:

public function register()
{
   return "This is register function";
}

The error you get is because if you remove RegisterRequest $requestfrom your function parameter, you cannot later use $request->email;because it's undefined

你得到的错误是因为如果你RegisterRequest $request从你的函数参数中删除,你以后不能使用,$request->email;因为它是未定义的