简体   繁体   English

如何使用Laravel 5.1创建简单的注册和登录?

[英]How to create simple Registration and login using Laravel 5.1?

I'm creating a simple registration form using a tutorial. 我正在使用教程创建一个简单的注册表格。 LINK I created everything mentioned there and additionally I added the Collective\\Html\\HtmlServiceProvider::class to my app.php . LINK我创建了这里提到的所有内容,此外,我还向我的app.php中添加了Collective \\ Html \\ HtmlServiceProvider :: class But when I run this app in my broswer using this url http://localhost/ptl/public/register i get this: 但是当我使用此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/controllers folder and my view in resources/views folder I'm confused about where to create the model class and how to use it with the controller and the view. 我的控制器在app / http / controllers文件夹中,我的视图在resources / views文件夹中,我对在哪里创建模型类以及如何在控制器和视图中使用模型类感到困惑。

In the console/terminal write: 在控制台/终端中输入:

php artisan make:auth

It will automaticly generate routes & views for login,forgotten password and registration 它将自动生成用于登录,忘记密码和注册的路线和视图

In laravel 5.1 there is a User Model by default. 在laravel 5.1中,默认情况下有一个用户模型。 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方法来注册/验证用户身份验证

// 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 这将路由到您的身份验证控制器,并使用trait来注册和验证用户

You can check Official Docs about 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM