一、下载 laravel 5.2
https://lulublog.cn/p/qErin2,进行操作
二、创建 admin migration
php artisan make:migration create_admin_table --create=admin
执行结果
Created Migration: 2023_06_29_111022_create_admin_table
可以在 database/migrations 目录下找到这个文件,修改文件
public function up()
{
Schema::create('admin', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
执行命令
php artisan migrate
三、后台登录注册
3.1、AdminAuth
新建文件夹:app/Http/Controllers/AdminAuth
复制 app/Http/Controllers/Auth 的文件到新建的目录,并修改命名空间
namespace App\Http\Controllers\AdminAuth;
修改 app/Http/Controllers/AdminAuth/AuthController
修改 redirectTo
protected $redirectTo = '/admin';
新增 guard 属性
protected $guard = 'admin';
删除 __construct 方法
添加两个方法
public function showLoginForm()
{
return view('admin.auth.login');
}
public function showRegistrationForm()
{
return view('admin.auth.register');
}
3.2、新建视图
新建文件夹 resources/views/admin,复制 resources/views/auth 到该目录
修改 resources/views/admin/auth/login.blade.php 的提交路由
/admin/login
修改 resources/views/admin/auth/register.blade.php 的提交路由
/admin/register
3.3、新建控制器
新建 AdminController
php artisan make:controller AdminController
修改 app/Http/Controllers/AdminController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('admin');
}
public function index()
{
return Auth::guard('admin')->user();
}
}
注意:可使用 check 检查是否登录
Auth::guard('admin')->check();
注意:可使用 logout 退出登录
Auth::guard('admin')->user()->logout();
3.4、新建模型
新建 AdminModel
php artisan make:model Admin
修改 app/Admin.php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
protected $table = 'admin';
protected $fillable = [
'name', 'email', 'password',
];
}
3.5、新建中间件
新建 RedirectIfNotAdmin
php artisan make:middleware RedirectIfNotAdmin
修改 app/Http/Middleware/RedirectIfNotAdmin.php
namespace App\Http\Middleware;
use Closure;
use Auth;
class RedirectIfNotAdmin
{
public function handle($request, Closure $next, $guard='admin')
{
if(!Auth::guard($guard)->check()){
return redirect('/');
}
return $next($request);
}
}
将 admin 增加到 app/Http/Kernel.php 的 routeMiddleware 属性
'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
3.6、配置 auth
配置 config/auth.php
guards 新增
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
providers 新增
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
passwords 新增
'admins' => [
'provider' => 'admins',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
3.7、新增路由
到 app/Http/routes.php 新增路由
Route::get('/admin/login', 'AdminAuth\AuthController@showLoginForm');
Route::post('/admin/login', 'AdminAuth\AuthController@login');
Route::get('/admin/logout', 'AdminAuth\AuthController@logout');
Route::get('/admin/register', 'AdminAuth\AuthController@showRegistrationForm');
Route::post('/admin/register', 'AdminAuth\AuthController@register');
Route::get('/admin', 'AdminController@index');
3.8、新建 admin 账号密码
php artisan tinker
namespace App;
$admin = new Admin
$admin->name = 'lulubin'
$admin->email = 'alulubin@gmail.com'
$admin->password = bcrypt('123456')
$admin->save();
3.9、测试
首先直接访问:http://localhost:8000/admin,会跳转 http://localhost:8000
然后访问:http://localhost:8000/admin/login,进行登录,登录成功后跳转 http://localhost:8000/admin