laravel-admin是一个快速构建后台管理的工具,提供页面组件和表单元素等功能,帮助快速实现功能以完善后台管理功能。
laravel-admin需PHP7+和Lavarel5.5+支持
安装
- 安装Composer包管理工具
Composer是PHP的依赖管理工具,类似于Node.js的npm命令
# Ubuntu操作系统安装Composer工具
$ sudo apt-get install composer
查看Composer当前版本
$ composer -V
Composer version 1.8.5 2019-04-09 17:46:47
修改Composer默认的镜像源地址为国内以加快访问速度
$ composer config -g repo.packagist composer https://packagist.lavarel-china.org
Composer默认的国外镜像源地址
$ composer config -g repo.packagist composer https://packagist.phpcomposer.com
- 使用Composer下载Laravel安装程序
$ composer global require "laravel/installer"
将Laravel安装路径添加到系统环境变量PATH中
Ubuntu操作系统下配置
$ echo export PATH="$PATH:~/.config/composer/vendor/bin">>~/.zshrc && source ~/.zshrc
Windows操作系统下默认Laravel的安装路径
C:\Users\[username]\AppData\Roaming\Composer\vendor\bin
- 使用Laravel创建项目
$ laravel new sxyh
创建项目出现错误
Crafting application...
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for symfony/css-selector v5.0.1 -> satisfiable by symfony/css-selector[v5.0.1].
- symfony/css-selector v5.0.1 requires php ^7.2.5 -> your PHP version (7.2.1) does not satisfy that requirement.
Problem 2
- Installation request for symfony/mime v5.0.1 -> satisfiable by symfony/mime[v5.0.1].
- symfony/mime v5.0.1 requires php ^7.2.5 -> your PHP version (7.2.1) does not satisfy that requirement.
Problem 3
- Installation request for symfony/service-contracts v2.0.1 -> satisfiable by symfony/service-contracts[v2.0.1].
- symfony/service-contracts v2.0.1 requires php ^7.2.5 -> your PHP version (7.2.1) does not satisfy that requirement.
Problem 4
- Installation request for symfony/translation-contracts v2.0.1 -> satisfiable by symfony/translation-contracts[v2.0.1].
- symfony/translation-contracts v2.0.1 requires php ^7.2.5 -> your PHP version (7.2.1) does not satisfy that requirement.
Problem 5
- symfony/service-contracts v2.0.1 requires php ^7.2.5 -> your PHP version (7.2.1) does not satisfy that requirement.
- symfony/console v4.4.1 requires symfony/service-contracts ^1.1|^2 -> satisfiable by symfony/service-contracts[v2.0.1].
- Installation request for symfony/console v4.4.1 -> satisfiable by symfony/console[v4.4.1].
说明当前PHP版本过低,需升级到7.2.5+。
初始化Laravel项目后可进入admin文件夹下使用php artisan
命令开启站点
$ php artisan serve
查看当前Laravel Installer版本
$ laravel -V
Laravel Installer 3.0.1
- 使用Composer安装laravel-admin插件
$ composer require encore/laravel-admin
- 使用
php artisan
命令发布资源
$ php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider"
发布后会生成config/admin.php
配置文件,在admin.php
文件中可配置数据库等信息。
- 配置数据库连接信息
在Ubuntu操作系统下使用命令安装MySQL数据库
$ sudo apt-get install mysql-server
进入项目config
文件夹下修改数据库配置文件
$ vim config/database.php
- 使用
php artisan
命令安装admin后台模块
$ php artisan admin:install
admin后台模块安装完成后会自动生成相关数据表和迁移文件,同时在app文件夹下会生成Admin后台模块。
配置
config/app.php
加入
Encore\Admin\Providers\AdminServiceProvider::class
发布资源
php artisan vendor:publish --tag=laravel-admin
完成安装
php artisan admin:install
测试
http://localhost/admin/
admin/admin
2. 模块开发
数据迁移与填充
php artisan make:migration create_categorys_table --create=categorys
Schema::create('categorys', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->nullabel()->default('');
$table->string('desc')->nullabel()->default('');
$table->string('ip')->nullabel()->default('0.0.0.0');
$table->string('flag')->unique();
$table->integer('pid')->nullable()->default('0');
$table->timestamps();
});
php artisan make:seeder CategorysTableSeeder
delete();
DB::table('categorys')->insert([
[
'id'=>100,
'name'=>'技术',
'pid'=>0,
'flag'=>'it',
'desc'=>'互联网,软件开发',
'ip'=>'127.0.0.1',
'created_at'=>'2017-06-20 02:30:10',
'updated_at'=>'2017-06-20 02:30:10',
],
[
'id'=>101,
'name'=>'随笔',
'pid'=>0,
'flag'=>'post',
'desc'=>'网络随笔',
'ip'=>'127.0.0.1',
'created_at'=>'2017-06-20 02:30:10',
'updated_at'=>'2017-06-20 02:30:10',
],
[
'id'=>102,
'name'=>'日记',
'pid'=>0,
'flag'=>'note',
'desc'=>'每日一记',
'ip'=>'127.0.0.1',
'created_at'=>'2017-06-20 02:30:10',
'updated_at'=>'2017-06-20 02:30:10',
],
]);
}
}
php artisan db:seed --class=CategorysTableSeeder
路由配置
app/Admin/routes.php
$router->resource('users', CategoryController::class);
创建MVC
# 创建模型
php artisan make:model \\Models\\Post
# 创建控制器并关联模型
php artisan admin:make CategoryController --model=App\\Models\\Category