Laravel-admin能夠快速構建一個功能強大的後臺,方便快速開發。php
如下內容記錄簡單使用Laravel-admin,以及遇到小錯誤的解決方法。laravel
Laravel-admin 依賴如下環境 須要提早裝好(安裝的Laravel-admin版本爲1.5)數據庫
1
2
3
|
Apache+PHP+MYSQL (這個不做解釋... 注意須要PHP 7+ 推薦使用phpstudy集成環境)
Laravel (5.5+)
Composer
|
以上環境如PHP/Composer須要設置好系統環境變量瀏覽器
先使用Composer命令安裝Laravel ,命令以下(用cmd或者Git先進入到想要安裝的目錄)bash
1
|
composer create-project --prefer-dist laravel
/laravel
Laravel-admin 5.7.*
|
若是以爲安裝速度慢,能夠改一下鏡像地址 參考:https://pkg.phpcomposer.com/app
安裝完成即顯示:composer
而後在安裝時指定的目錄會有一個Laravel-admin文件夾 此時須要設置一下Apache 網站目錄設置爲Laravel-admin下的public編輯器
使用的是phpstudy集成環境,因此在phpstudy中點擊[其餘選項菜單]=>[站點域名管理] 設置內容:ide
接下來須要設置一下系統hosts,使用集成環境phpstudy能夠在[其餘選項菜單]=>[打開host]函數
也能夠手動打開該文件,該文件路徑爲:
1
|
C:Windows\System32\driversetc\hosts
|
在hosts文件最底下添加一行代碼:
1
|
127.0.0.1 www.laravel-admin.
test
|
保存好文件,打開瀏覽器輸入www.laravel-admin.test便可看到Laravel頁面:
若是不能顯示這個界面,請檢查是否漏掉了上面某個步驟。
接下來須要設置Laravel的數據庫
先使用瀏覽器打開localhost/phpmyadmin/ 此數據庫管理系統是phpstudy自帶了
使用帳號/密碼root登陸進去
新建一個數據庫:
數據庫建好後,打開Laravel-admin目錄,修改該目錄下的.env文件,修改配置數據庫參數:
接下來就是安裝Laravel-admin,打開Git或者cmd指定到Laravel-admin目錄下,而後輸入如下代碼(也可打開Laravel-admin官方 參考官方文檔安裝):
1
|
composer require encore
/laravel-admin
|
完成後:
使用命令發佈資源:
1
|
php artisan vendor:publish --provider=
"Encore\Admin\AdminServiceProvider"
|
最後使用命令完成安裝
1
|
php artisan admin:
install
|
若是出現錯誤:
1
2
3
4
5
|
1 DoctrineDBALDriverPDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Spec
ified key was too long; max key length is 1000 bytes")
2 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too lon
g; max key length is 1000 bytes")
|
解決方法:
修改laravel-admin/app/Providers目錄下的AppServiceProvider.php文件:
代碼:
1
2
|
use
Illuminate\Support\Facades\Schema;
Schema::defaultStringLength(191);
|
再進入數據庫管理系統 localhost/phpmyadmin/ 將laravel_admin數據庫裏面的表全刪除
再執行命令:
1
|
php artisan admin:
install
|
若是沒有遇到錯誤,則無需修改文件也無需執行命令。
至此,Laravel-admin已經安裝完成
能夠在瀏覽器打開:www.laravel-admin.test/admin 訪問進入後臺。帳號及密碼爲:admin
假如數據庫中有一個文章表,如何使用Laravel-admin管理文章?
接下來簡單使用Laravel-admin管理文章。
先在laravel_admin數據庫創建一個文章表 表取名爲:Article,
須要的字段有:
id(設置爲主鍵並自增),
title(文章標題,VARCHAT類型長度32),
content(文章內容,TEXT類型),
updated_at(修改時間,TIMESTAMP類型),
created_at(建立時間,TIMESTAMP類型)
--此處犯了個錯誤,表名不應使用大寫開頭,應該使用全小寫(開發規範) 你們引覺得戒。
此時打開後臺,會發現都是英文,須要修改配置文件(laravel-admin目錄下)config/app.php:
找到代碼:
1
|
'locale'
=>
'en'
,
|
修改成:
1
|
'locale'
=>
'zh-CN'
,
|
此時打開後臺依然會發現左側導航存在英文
點擊左側導航中的[Admin]=>[Menu],便可修改導航爲中文(須要一個一個修改左側導航)。
回到重點,須要建立一個菜單,爲文章管理
此時刷新頁面,會發現左側導航有一個【文章管理】,若是點擊【文章管理】會發現報404錯誤,這是由於沒有設置路由。
打開文件(laravel-admin目錄下)app/Admin/routes.php
添加一句代碼:
1
|
$router
->resource(
'/article'
, ArticleController::
class
);
|
進入laravel-admin目錄下app/Admin/Controllers
複製控制器文件ExampleController.php爲ArticleController.php
修改類名:
1
|
class
ExampleController
extends
Controller
|
將類名修改成ArticleController
如:
1
|
class
ArticleController
extends
Controller
|
建立文章表模型,在cmd或者Git使用命令:
1
|
php artisan
make
:model Article
|
此時會生成一個名爲Article.php的文件並存放於laravel-admin目錄下app目錄中。
須要爲此文件的Article類中添加代碼:
1
|
protected
$table
=
'Article'
;
|
添加後Article.php文件的代碼如:
1
2
3
4
5
6
7
8
9
10
|
<?php
namespace
App;
use
Illuminate\Database\Eloquent\Model;
class
Article
extends
Model
{
protected
$table
=
'Article'
;
}
|
繼續修改laravel-admin目錄下app/Admin/Controllers/ArticleController.php文件
將全部寫着YourModel的地方修改成Article
別忘了須要在文件頂部引入文章模型,引入代碼:
1
|
use
App\Article;
|
此時後臺左側導航的文章管理已經能正常打開不會報404錯誤
爲了這個界面好看點,在app/Admin/Controllers/ArticleController.php文件的grid函數修改以下:
1
2
3
4
5
6
7
8
9
10
11
|
protected
function
grid()
{
$grid
=
new
Grid(
new
Article);
$grid
->id(
'ID'
)->sortable();
$grid
->title(
'文章標題'
);
$grid
->created_at(
'建立時間'
);
$grid
->updated_at(
'修改時間'
);
return
$grid
;
}
|
此時刷新界面再點擊右上角【新建】
須要修改form函數:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
protected
function
form()
{
$form
=
new
Form(
new
Article);
$form
->text(
'title'
,
'文章標題'
);
$form
->textarea(
'content'
,
'文章內容'
);
$form
->display(
'id'
,
'文章ID'
);
$form
->display(
'created_at'
,
'建立時間'
);
$form
->display(
'updated_at'
,
'修改時間'
);
return
$form
;
}
|
此時再刷新頁面,發現有了一些組件,能夠正常寫文章並提交保存到數據庫。
返回到文章管理的頁面,點擊右側【操做】中的「眼睛」按鈕查看文章詳情,發現並無文章詳情,這是由於須要修改detail函數:
1
2
3
4
5
6
7
8
9
10
11
12
|
protected
function
detail(
$id
)
{
$show
=
new
Show(Article::findOrFail(
$id
));
$show
->id(
'文章ID'
);
$show
->title(
'文章標題'
);
$show
->content(
'文章內容'
);
$show
->created_at(
'建立時間'
);
$show
->updated_at(
'修改時間'
);
return
$show
;
}
|
laravel-admin目錄下app/Admin/Controllers/ArticleController.php控制器所有代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
<?php
namespace
AppAdminControllers;
use
App\Http\Controllers\Controller;
use
Encore\Admin\Controllers\Has\Resource\Actions;
use
Encore\Admin\Form;
use
Encore\Admin\Grid;
use
Encore\Admin\Layout\Content;
use
Encore\Admin\Show;
use
App\Article;
class
ArticleController
extends
Controller
{
use
Has\Resource\Actions;
/**
* Index interface.
*
* @param Content $content
* @return Content
*/
public
function
index(Content
$content
)
{
return
$content
->header(
'Index'
)
->description(
'description'
)
->body(
$this
->grid());
}
/**
* Show interface.
*
* @param mixed $id
* @param Content $content
* @return Content
*/
public
function
show(
$id
, Content
$content
)
{
return
$content
->header(
'Detail'
)
->description(
'description'
)
->body(
$this
->detail(
$id
));
}
/**
* Edit interface.
*
* @param mixed $id
* @param Content $content
* @return Content
*/
public
function
edit(
$id
, Content
$content
)
{
return
$content
->header(
'Edit'
)
->description(
'description'
)
->body(
$this
->form()->edit(
$id
));
}
/**
* Create interface.
*
* @param Content $content
* @return Content
*/
public
function
create(Content
$content
)
{
return
$content
->header(
'Create'
)
->description(
'description'
)
->body(
$this
->form());
}
/**
* Make a grid builder.
*
* @return Grid
*/
protected
function
grid()
{
$grid
=
new
Grid(
new
Article);
$grid
->id(
'ID'
)->sortable();
$grid
->title(
'文章標題'
);
$grid
->created_at(
'建立時間'
);
$grid
->updated_at(
'修改時間'
);
return
$grid
;
}
/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected
function
detail(
$id
)
{
$show
=
new
Show(Article::findOrFail(
$id
));
$show
->id(
'文章ID'
);
$show
->title(
'文章標題'
);
$show
->content(
'文章內容'
);
$show
->created_at(
'建立時間'
);
$show
->updated_at(
'修改時間'
);
return
$show
;
}
/**
* Make a form builder.
*
* @return Form
*/
protected
function
form()
{
$form
=
new
Form(
new
Article);
$form
->text(
'title'
,
'文章標題'
);
$form
->textarea(
'content'
,
'文章內容'
);
$form
->display(
'id'
,
'文章ID'
);
$form
->display(
'created_at'
,
'建立時間'
);
$form
->display(
'updated_at'
,
'修改時間'
);
return
$form
;
}
}
|
至此,文章管理中的CRUD(增刪查改)均實現。
但仔細看,後臺中的新建文章的文章內容使用的是textarea而不是富文本編輯器。
官方文檔有詳細使用富文本編輯器的教程:https://laravel-admin.org/docs/zh/model-form-field-management
後續若是有須要再寫富文本編輯器的文章吧。