一:前提
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,然後學習它的源碼和代碼設計,最後取其精華,為你所用。