如何使用 Laravel 5.1 创建简单的注册和登录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33775466/
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
How to create simple Registration and login using Laravel 5.1?
提问by Hamelraj
I'm creating a simple registration form using a tutorial.LINKI created everything mentioned there and additionally I added the Collective\Html\HtmlServiceProvider::classto my app.php. But when I run this app in my broswer using this url http://localhost/ptl/public/register
i get this:
我正在使用教程创建一个简单的注册表单。LINK我创建了那里提到的所有内容,另外我将Collective\Html\HtmlServiceProvider::class 添加到我的app.php。但是当我使用这个 url 在我的浏览器中运行这个应用程序时,我http://localhost/ptl/public/register
得到了这个:
<form method="POST" action="http://localhost/ptl/public/register_action" accept-charset="UTF-8"><input name="_token" type="hidden" value="roIXm176ZnQyTyYcXjm7Qif7mg2NXdIX0lCZ55z0">
Name :
<input name="name" type="text">
Email :
<input name="email" type="text">
Password :
<input name="password" type="password" value="">
Confirm Password :
<input name="cpassword" type="password" value="">
<input type="submit" value="Submit">
</form>
and my Controller in app/http/controllersfolder and my view in resources/views folder I'm confused about where to create the modelclass and how to use it with the controller and the view.
和我在app/http/controllers文件夹中的控制器和我在资源/视图文件夹中的视图我对在哪里创建模型类以及如何将它与控制器和视图一起使用感到困惑。
采纳答案by vpedrosa
You can check Official Docsabout Authentication and Laracast free explaining video
您可以查看有关身份验证和Laracast 免费解释视频的官方文档
Sample Routes:
示例路线:
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
Sample Authentication form view:
示例身份验证表单视图:
<!-- resources/views/auth/login.blade.php -->
<form method="POST" action="/auth/login">
{!! csrf_field() !!}
<div>
Email
<input type="email" name="email" value="{{ old('email') }}">
</div>
<div>
Password
<input type="password" name="password" id="password">
</div>
<div>
<input type="checkbox" name="remember"> Remember Me
</div>
<div>
<button type="submit">Login</button>
</div>
</form>
Sample Registration Form view:
样本注册表视图:
<!-- resources/views/auth/login.blade.php -->
<form method="POST" action="/auth/login">
{!! csrf_field() !!}
<div>
Email
<input type="email" name="email" value="{{ old('email') }}">
</div>
<div>
Password
<input type="password" name="password" id="password">
</div>
<div>
<input type="checkbox" name="remember"> Remember Me
</div>
<div>
<button type="submit">Login</button>
</div>
</form>
回答by Maky
In the console/terminal write:
在控制台/终端写:
php artisan make:auth
It will automaticly generate routes & views for login,forgotten password and registration
它会自动生成用于登录、忘记密码和注册的路线和视图
回答by Hakob Hakobyan
In laravel 5.1 there is a User Model by default. and you can add your models anywhere in app directory(they live there See official documentation), You can also use default laravel methods to register/authenticate users Authentication
在 laravel 5.1 中默认有一个用户模型。你可以在 app 目录的任何地方添加你的模型(他们住在那里见官方文档),你也可以使用默认的 Laravel 方法来注册/验证用户身份验证
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
to your routings. This will route to your auth controllers and use trait for registrating and authenticating users
到您的路线。这将路由到您的身份验证控制器并使用特征来注册和验证用户