项目用到的前端库hdjs网址hdjs.hdphp.com
一、安装提示增强工具Laravel-ide-helper
1、下载:composer require barryvdh/laravel/laravel-ide-helper
2、在app.php下 providers添加:Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
3、php artisan ide-helper:generate(如果没有效果重新启动IDE,Phpstorm)
二、数据填充与数据迁移
1、创建hd表
php artisan make:migration create_tb_table --create=hd
2、创建模型并创建表
php artisan make:model Model/Admin -m
3.使用tinker与factory创建管理员初始数据
php artisan tinker
factory (App\Model\Admin\::class,3)->create();
4.数据回滚
php artisan migrate:rollback
三、解决mysql5.7以下版本不能数据迁移两种解决方案
1,
config文件夹下database.php
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
修改
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
2、
app/Providers文件夹下
AppServiceProvider.php
use Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
四、使用用户认证系统与独立设置guard进行登录处理
在config/auth.php
'guards'=>[],
添加
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
在'providers' => [],
添加
'admins' => [
'driver' => 'eloquent',
'model' => App\Model\Admin::class,
],
在Model层添加
use Illuminate\Foundation\Auth\User;
模型继承User
五、使用中间件mideleware进项行权限登录验证
1、创建中间件
php artisan make:middleware AdminMiddleware
2、找到 Http\Middleware下的AdminMiddleware.php
引入 use Auth;
添加判断
if(!Auth::guard('admin')->check()){
return redirect('/admin/login');
}
3、找到Http\kernel.php
在路由中间件 $routeMiddleware添加
'admin.auth'=>AdminMiddleware::class,
4.使用
在登录类
public function __construct()
{
$this->middleware('admin.auth')->except(['loginForm','login']);
//excepts是哪个方法不执行
}
六、退出报错
QueryException in Connection.php line 647:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'remember_token' in 'field list' (SQL: update `admins` set `remember_token` = 4rwLxz0HkIuhgE5DTojXqQ7OrE9IZcBae3vIgKfijsxtVMr33SDdvi7aGrhg where `id` = 1)
在Admin.php
添加
protected $rememberTokenName = ' ';
七、使用Request请求验证&bootstrap进行表单验证处理
php artisan make request AdminPost
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Auth;
use Validator;
use Hash;
class AdminPost extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return Auth::guard('admin')->check();
}
/**
* 添加验证规则
*/
public function addValidator()
{
//验证用户密码
Validator::extend('check_password', function ($attribute, $value, $parameters, $validator) {
return Hash::check($value, Auth::guard('admin')->user()->password);
});
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$this->addValidator();
return [
'password' => 'sometimes|required|confirmed',
'password_confirmation' => 'sometimes|required',
'original_password' => 'sometimes|required|check_password',
];
}
/**
* 中文提示
*
* @return array
*/
public function messages()
{
return [
'password.required' => '新密码不能为空',
'password_confirmation.required' => '确认密码不能为空',
'password.confirmed' => '两次密码输入不一致',
'original_password.required' => '原密码输入错误',
'original_password.check_password' => '原密码输入错误',
];
}
}
八、组件提示消息flash
1、安装
composer require laracasts/flash
2、添加到app.php providers下
Laracasts\Flash\FlashServiceProvider::class,
3、master.blade.php引入
@include(‘flash::message’)
添加
require(['bootstrap'],function ($) {
$('#flash-overlay-modal').modal();
});
4、执行
php artisan vendor:publish --provider="Laracasts\Flash\FlashServiceProvider"
九、创建资源管理器
php artisan make:controller Admin\TagController --resource
网友评论