Home > php教程 > PHP开发 > Detailed explanation of the steps to build a backend login system in Laravel

Detailed explanation of the steps to build a backend login system in Laravel

高洛峰
Release: 2016-12-23 17:14:48
Original
1527 people have browsed it

The example in this article describes how to build a backend login system in Laravel. I share it with you for your reference. The details are as follows:

Today I want to use laravel to build a backend system. I need the simplest one. There is a user login system. After trying it out, I feel that laravel’s user login is really happy. . Of course, the premise is that the user management system you want is the simplest one, that is, you only need to be able to log in without user permissions.

I won’t use the default user table as an example here, because it would be easy to be confused with some of Laravel’s default settings.

First confirm, the user table in the background, I designed the table to be called badin. Each administrator has a username (username), a nickname (nickname), an email address (email), and a password (password)

Have fun here , use laravel's migration to create the table (actually you don't need to use this tool to create the table)

1 Install the most basic laravel framework

2 Create the migration file:

./artisan migrate:make create-badmin-table

3 Found an additional php file under app/database/migration/:

2014_10_19_090336_create-badmin-table.php

4 Add content to up and down;

<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBadminTable extends Migration {
   /**
   * Run the migrations.
   *
   * @return void
   */
   public function up()
   {
     Schema::create(&#39;badmin&#39;, function($table)
     {
       $table->increments(&#39;id&#39;);
        $table->string(&#39;nickname&#39;, 100)->unique();
        $table->string(&#39;username&#39;, 100)->unique();
        $table->string(&#39;email&#39;, 100)->unique();
        $table->string(&#39;password&#39;, 64);
        $table->timestamps();
     });
   }
   /**
   * Reverse the migrations.
   *
   * @return void
   */
   public function down()
   {
     Schema::drop(&#39;badmin&#39;);
   }
}
Copy after login

5 Configure the local database , app/config/local/database.php

<?php
return array(
  &#39;fetch&#39; => PDO::FETCH_CLASS,
  &#39;default&#39; => &#39;mysql&#39;,
  &#39;connections&#39; => array(
    &#39;mysql&#39; => array(
      &#39;driver&#39;  => &#39;mysql&#39;,
      &#39;host&#39;   => &#39;localhost&#39;,
      &#39;database&#39; => &#39;test&#39;,
      &#39;username&#39; => &#39;yejianfeng&#39;,
      &#39;password&#39; => &#39;123456&#39;,
      &#39;charset&#39;  => &#39;utf8&#39;,
      &#39;collation&#39; => &#39;utf8_unicode_ci&#39;,
      &#39;prefix&#39;  => &#39;&#39;,
    ),
  ),
  &#39;migrations&#39; => &#39;migrations&#39;,
);
Copy after login

6 Create a data table:

./artisan migrate --env=local

At this time, I went to the database and found that there was an additional badmin table. The data structure is as follows:

CREATE TABLE `badmin` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `nickname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `username` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `password` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
 `created_at` timestamp NOT NULL DEFAULT &#39;0000-00-00 00:00:00&#39;,
 `updated_at` timestamp NOT NULL DEFAULT &#39;0000-00-00 00:00:00&#39;,
 PRIMARY KEY (`id`),
 UNIQUE KEY `badmin_nickname_unique` (`nickname`),
 UNIQUE KEY `badmin_username_unique` (`username`),
 UNIQUE KEY `badmin_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Copy after login

I want to ask why create_at and update_at are added here. These are the fields created by laravel for each table by default, and these two can be automatically updated when using Eloquent for addition, deletion, modification and query. Field

7 Create a Model:

<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class Badmin extends Eloquent implements UserInterface, RemindableInterface {
   use UserTrait, RemindableTrait;
   protected $table = &#39;badmin&#39;;
   protected $hidden = array(&#39;password&#39;);
   public static $rules = [
     &#39;nickname&#39; => &#39;required|alpha_num|min:2&#39;,
     &#39;username&#39; => &#39;required&#39;,
     &#39;email&#39;=>&#39;required|email|unique:badmin&#39;,
     &#39;password&#39;=>&#39;required|alpha_num|between:6,12|confirmed&#39;,
   ];
}
Copy after login

You must implement UserInterface and RemindableInterface here

8 Associate the model with Auth, modify app/config/auth.php

<?php
return array(
   // 默认的用户验证驱动
   // 可以是database或者eloquent
   &#39;driver&#39; => &#39;eloquent&#39;,
   // 只有驱动为eloquent的时候才有用
   &#39;model&#39; => &#39;Badmin&#39;,
);
Copy after login

The driver here It can be eloquent or database. Using eloquent tells the Auth component that the user authentication class is managed by the Badmin class. The model here has a namespace, that is to say, if your admin class is YejianfengBadmin, it should be changed to 'YejianfengBadmin'

9 Okay, at this time, the logic part has actually been built, and you can already use it in the controller

Auth::attempt(XXX) does permission authentication

Auth::user() gets the logged in user (a Badmin class)
etc.

10 Next, create a user login page:

Detailed explanation of the steps to build a backend login system in Laravel

11 Set routing:

<?php
// 不需要登录验证的接口
Route::get(&#39;/&#39;, [&#39;as&#39; => &#39;user.login&#39;,&#39;uses&#39;=>&#39;UserController@getLogin&#39;]);
Route::get(&#39;user/login&#39;, [&#39;as&#39; => &#39;login&#39;, &#39;uses&#39; => &#39;UserController@getLogin&#39;]);
Route::post(&#39;user/login&#39;, [&#39;as&#39; => &#39;login&#39;, &#39;uses&#39; => &#39;UserController@postLogin&#39;]);
// 需要登录验证才能操作的接口
Route::group(array(&#39;before&#39; => &#39;auth&#39;), function()
{
  Route::get(&#39;user/logout&#39;, [&#39;as&#39; => &#39;logout&#39;, &#39;uses&#39; => &#39;UserController@getLogout&#39;]);
  Route::get(&#39;user/dashboard&#39;, [&#39;as&#39; => &#39;dashboard&#39;, &#39;uses&#39; => &#39;UserController@getDashboard&#39;]);
});
Copy after login

12 Set controller:

<?php
class UserController extends BaseController {
   // 登录页面
   public function getLogin()
   {
     return View::make(&#39;user.login&#39;);
   }
   // 登录操作
   public function postLogin()
   {
     if (Auth::attempt(array(&#39;email&#39;=>Input::get(&#39;email&#39;), &#39;password&#39;=>Input::get(&#39;password&#39;)))) {
       return Redirect::to(&#39;user/dashboard&#39;)
       ->with(&#39;message&#39;, &#39;成功登录&#39;);
     } else {
       return Redirect::to(&#39;user/login&#39;)
          ->with(&#39;message&#39;, &#39;用户名密码不正确&#39;)
          ->withInput();
     }
   }
   // 登出
   public function getLogout()
   {
     Auth::logout();
     return Redirect::to(&#39;user/login&#39;);
   }
   public function getDashboard()
   {
     return View::make(&#39;user.dashboard&#39;);
   }
   // 添加新用户操作
   public function getCreate()
   {
     return View::make(&#39;user.create&#39;);
   }
   // 添加新用户操作
   public function postCreate()
   {
     $validator = Validator::make(Input::all(), User::$rules);
     if ($validator->passes()){
        $bAdmin = new Badmin();
        $bAdmin->nickname = Input::get(&#39;nickname&#39;);
        $bAdmin->username = Input::get(&#39;username&#39;);
        $bAdmin->email = Input::get(&#39;email&#39;);
        $user->password = Hash::make(Input::get(&#39;password&#39;));
        $user->save();
        Response::json(null);
     } else {
        Response::json([&#39;message&#39; => &#39;注册失败&#39;], 410);
     }
   }
}
Copy after login

13 Set filter, app/fil ter.php

Route::filter(&#39;auth&#39;, function()
{
   if (Auth::guest())
   {
     if (Request::ajax())
     {
        return Response::make(&#39;Unauthorized&#39;, 401);
     }
     else
     {
        return Redirect::guest(&#39;/&#39;);
     }
   }
});
Copy after login

Change the address after authentication failure to / path

14 Set views/user/login.blade.php

As you can see, Session::has and Session::get can be used directly here

Then it's basically done...

I hope this article will be helpful to everyone's PHP program design based on the Laravel framework.

For more detailed information on the steps to build a backend login system in Laravel, please pay attention to the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template