Laravel-admin安装实证过程

1 篇文章 0 订阅
1 篇文章 0 订阅

一:前提 

php环境 php>7.1

compose

nodejs

二、

1. laravel-admin安装的前提是laravel5.8.3已经安装完毕:(安装到LaravelWEB目录下)

doc地址:http://laravel-admin.org/docs/#/zh/installation

首先确保安装好了laravel,并且数据库连接设置正确。

composer create-project --prefer-dist laravel/laravel LaravelWEB

2.laravel-admin需在laravel的根目录下进行安装:

cd  LaravelWEB

3.laravel-admin的安装命令:

composer require encore/laravel-admin "1.6.*" #用composer安装laravel-admin
php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider" #发布资源,在该命令会生成配置文件config/admin.php,可以在里面修改安装的地址、数据库连接、以及表名,建议都是用默认配置不修改。
php artisan admin:install #安装laravel-admin并进行数据库迁移 

运行install时,可能会报mysql1071错误,mysql支持数据库表单一键值的最大长度不能超过767字节,超出这个长度即报错

解决: 找到app/Provides/AppServiceProvides.php

引入命名: 

use Illuminate\Support\Facades\Schema;

限制长度

public function boot()
{
    //
    Schema::defaultStringLength(191);
}

到此,laravel admin 安装完毕
 

4.进入到laravel根目录,找到.env文件,修改数据库配置

5.进入到laravel根目录的config文件夹,找到database.php文件,更改数据库配置

6配置虚拟目录:

          C盘找到windows>>system32>>drivers>>etc>>hosts

          进入hosts文件,在最后,对127.0.0.1解除注释,并把127.0.0.1映射域名  

      若是window系统,Apache环境:

          C盘找到Xampp>>apache>>conf>>extra>>httpd-vhosts.conf

          进入文件把最后一部分注释取消,DocumentRoot对应域名所在项目的根目录,ServerName 对应 域名,ServerAlias 对应 www.域名

      若是window系统,nginx环境:

server {
        listen       80;
        server_name  laravel.com lijin37.com;
        root   "D:\phpStudy\PHPTutorial\WWW\LaracelWEB\public";
        location / {
            index  index.html index.htm index.php;           if (!-e $request_filename) {                    rewrite  ^(.*)$  /index.php?s=/$1  last;                    break;   }
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

7.运行Apache/Nginx,网页输入,域名/index.php/admin   进入laravel-admin登陆页 用户名:admin 密码:admin进入主页,配置完成。进入后台: 侧边栏有index和admin选项,里面包括管理员管理(Users)、权限管理(Roles、Perimession)、后台菜单管理(Menu)、操作日志(Operation Log)

在首页是由用到的一下插件和环境信息

四: 开始搭建后台

1: 首先按照laravel config,这是每个后台都需要用到的网站配置管理,我们可以使用laravel admin为我们提供的现成的

http://laravel-admin.org/docs/#/zh/extension-config

php artisan migrate

同样在app/Providers/AppServiceProvider.php的boot中添加Config::load()

public function boot()
    {
        //
        Schema::defaultStringLength(191);
        Config::load();
    }


最后运行命令导入菜单和权限(也可以手动添加)

php artisan admin:import config

这样一个网站配置管理就搞定了

2: 帮助工具

laravel admin 提供了脚手架,可以帮助我们快速搭建后台

http://laravel-admin.org/docs/#/zh/extension-helpers

composer require laravel-admin-ext/helpers

php artisan admin:import helpers

到此我们的准备工作就做好了,下一步我们将使用脚手架快速搭建一个后台!

 

上传头像发现路径不对,找到config/app.php 里

(注意env文件优先级更高,可能架空config)

'url' => env('APP_URL', 'http://localhost'), //改成自己的域名

'debug' => env('APP_DEBUG', false),//想调试改为true

汉化:

 'locale' => 'zh-CN',

出现

Disk [admin] not configured, please add a disk config in `config/filesystems.php`.

在config/filesystems.php中disks内添加:

 'admin' => [
        'driver'     => 'local',
        'root'       => public_path('upload'),
        'visibility' => 'public',
        'url' => env('APP_URL').'/public/upload/',
    ],

如果是把网址直接指向了public,请将上面的/public去掉

 

导出excel里中文乱码,找到\vendor\encore\laravel-admin\src\Grid\Exporters\CsvExporter.php 原因是BOM

*/
    public function export()
    {
        $filename = $this->getTable().'.csv';
        print(chr(0xEF).chr(0xBB).chr(0xBF));        //乱码问题解决
             $headers = [
            'Content-Encoding'    => 'UTF-8',
            'Content-Type'        => 'text/csv;charset=UTF-8',
            'Content-Disposition' => "attachment; filename=\"$filename\"",
        ];

 

 

代码主要集中在\APP\Admin中

默认系统提供一个 Dashboard 界面:

<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;

class HomeController extends Controller
{
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('Test Dashboard');
            $content->description('Description...');

            $content->row(Dashboard::title());

            $content->row(function (Row $row) {

                $row->column(4, function (Column $column) {
                    $column->append(Dashboard::environment());
                });

                $row->column(4, function (Column $column) {
                    $column->append(Dashboard::extensions());
                });

                $row->column(4, function (Column $column) {
                    $column->append(Dashboard::dependencies());
                });
            });
        });
    }
}

结合界面和代码,可以看出界面主要分成这么几个部分:header、description、两个 row,后一个 row 包含三个 column 模块;具体的代码放在 Dashboard 代码中,如下:

<?php

namespace Encore\Admin\Controllers;

use Encore\Admin\Admin;

class Dashboard
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public static function title()
    {
        return view('admin::dashboard.title');
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public static function environment()
    {
        $envs = [
            ['name' => 'PHP version',       'value' => 'PHP/'.PHP_VERSION],
            ['name' => 'Laravel version',   'value' => app()->version()],
            ['name' => 'CGI',               'value' => php_sapi_name()],
            ['name' => 'Uname',             'value' => php_uname()],
            ['name' => 'Server',            'value' => array_get($_SERVER, 'SERVER_SOFTWARE')],

            ['name' => 'Cache driver',      'value' => config('cache.default')],
            ['name' => 'Session driver',    'value' => config('session.driver')],
            ['name' => 'Queue driver',      'value' => config('queue.default')],

            ['name' => 'Timezone',          'value' => config('app.timezone')],
            ['name' => 'Locale',            'value' => config('app.locale')],
            ['name' => 'Env',               'value' => config('app.env')],
            ['name' => 'URL',               'value' => config('app.url')],
        ];

        return view('admin::dashboard.environment', compact('envs'));
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public static function extensions()
    {
        $extensions = [
            'helpers' => [
                'name' => 'laravel-admin-ext/helpers',
                'link' => 'https://github.com/laravel-admin-extensions/helpers',
                'icon' => 'gears',
            ],
            'log-viewer' => [
                'name' => 'laravel-admin-ext/log-viewer',
                'link' => 'https://github.com/laravel-admin-extensions/log-viewer',
                'icon' => 'database',
            ],
            'backup' => [
                'name' => 'laravel-admin-ext/backup',
                'link' => 'https://github.com/laravel-admin-extensions/backup',
                'icon' => 'copy',
            ],
            'config' => [
                'name' => 'laravel-admin-ext/config',
                'link' => 'https://github.com/laravel-admin-extensions/config',
                'icon' => 'toggle-on',
            ],
            'api-tester' => [
                'name' => 'laravel-admin-ext/api-tester',
                'link' => 'https://github.com/laravel-admin-extensions/api-tester',
                'icon' => 'sliders',
            ],
            'media-manager' => [
                'name' => 'laravel-admin-ext/media-manager',
                'link' => 'https://github.com/laravel-admin-extensions/media-manager',
                'icon' => 'file',
            ],
            'scheduling' => [
                'name' => 'laravel-admin-ext/scheduling',
                'link' => 'https://github.com/laravel-admin-extensions/scheduling',
                'icon' => 'clock-o',
            ],
            'reporter' => [
                'name' => 'laravel-admin-ext/reporter',
                'link' => 'https://github.com/laravel-admin-extensions/reporter',
                'icon' => 'bug',
            ],
            'translation' => [
                'name' => 'laravel-admin-ext/translation',
                'link' => 'https://github.com/laravel-admin-extensions/translation',
                'icon' => 'language',
            ],
        ];

        foreach ($extensions as &$extension) {
            $name = explode('/', $extension['name']);
            $extension['installed'] = array_key_exists(end($name), Admin::$extensions);
        }

        return view('admin::dashboard.extensions', compact('extensions'));
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public static function dependencies()
    {
        $json = file_get_contents(base_path('composer.json'));

        $dependencies = json_decode($json, true)['require'];

        return view('admin::dashboard.dependencies', compact('dependencies'));
    }
}

这样我们就把代码分块的组织在一起。具体布局类看:class Content implements Renderable

其它的静态资源文件放在 /public/vendor/laravel-admin 目录下

更多内容参考 laravel-admin 官网:
http://laravel-admin.org/docs/#/zh/

 

 

 

写一个 demo

有了这个 laravel-admin 插件,要写一个 movies 列表,只需要几个命令行就可以完成了,非常简单:

1.建立模型,并创建 Migrations:

php artisan make:model Movie -m 

2.在 Migrations(D:\phpStudy\PHPTutorial\WWW\LaracelWEB\database\migrations),增加一个字段:name

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('movies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->unique();//这里这里
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('movies');
    }
}

3.运行 Migrations,创建对应数据库:

php artisan migrate

4.有了数据表,就需要往表里插入 fake 数据,用于测试

// 使用该插件创建 fake 数据
composer require fzaninotto/faker 

5.建立 Seeder

php artisan make:seeder MovieTableSeeder 

在该类中,建立1000条假数据:

<?php

use Illuminate\Database\Seeder;

class MovieTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        $faker = Faker\Factory::create();

        for($i = 0; $i < 1000; $i++) {
            App\Movie::create([
                'name' => $faker->name
            ]);
        }
    }
}

运行:

php artisan db:seed --class=MovieTableSeeder 

是不是很简单,数据表直接填充 1000 条假数据:

6.建立资源 Controller

php artisan admin:make MovieController --model=App\Movie 

(注释:模型路径里, 两个斜杠是homestead的写法,一个斜杠是windows下的写法)

这样就直接有了基础的增删改查和 movie 列表功能的 Controller 了。

7.添加路由

app/Admin/routes.php中添加路由:

$router->resource('movies', MovieController::class);

8.加入到 admin 的 menu 中:

路径填写movies

其中路径处需要注意的是:

其中uri填写不包含路由前缀的的路径部分,比如完整路径是http://localhost:8000/admin/demo/users, 那么就填demo/users,如果要添加外部链接,只要填写完整的url即可,比如http://laravel-admin.org/.

上图也是加了左侧 movies 菜单的效果。

就完成了简单的 movie 资源的后台管理了,在浏览器输入链接:
http://web.app/admin/movies

就能看到一个较为完整的 movie 列表:

 

具体有新增、导出、筛选、操作 (删除)、撤销、分页、修改、删除等常规功能,如下几个截图:

总结

有了 Laravel 和 laravel-admin,基本不用写什么代码,敲敲几个命令就可以完成一个「功能比较齐全」的资源操作后台。极大的方便了我们的开发。

总体命令行和代码如下

php artisan make:model Movie -m

php artisan migrate

composer require fzaninotto/faker

php artisan make:seeder MovieTableSeeder

php artisan db:seed --class=MovieTableSeeder

php artisan admin:make MovieController --model=App\Movie

$router->resource('movies', MovieController::class);

框架和开源插件,有时候确实是能方便我们开发,所以寻找优质的框架和开源库也是促进我们生产力的。

laravel-admin 代码是如何组织的,可以具体参考网站开发。先根据官网的介绍,利用好 laravel-admin,然后学习它的源码和代码设计,最后取其精华,为你所用。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值