TP6框(kuang)架--EasyAdmin學習筆記:定義(yi)路由(you)
這是我寫的學習EasyAdmin的第二章,這一章我給大家分享下如何定義一條路由
正常(chang)的tp6定(ding)義路由方法如(ru)下:

/route/admins/app.php 文件內(nei)容(rong)
//路由變量自定義
Route::get('zhanghao/denglu/[:aaa]','app\\admins\\controller\\Account@login');
\app\admins\controller\Account.php 文件(jian)內(nei)容
<?php
namespace app\admins\controller;
use think\facade\Db;
use app\BaseController;
class Account extends BaseController{
public function login($aaa){
return $aaa;
return view();
}
}
以上是(shi)tp6的(de)(de)路(lu)由(you)定(ding)義(yi)流程,我們可以看出,tp6定(ding)義(yi)路(lu)由(you)是(shi)view+model+controller三層文(wen)件(jian)(jian)組(zu)成的(de)(de),而EasyAdmin里(li)運(yun)用(yong)了layui框架,在(zai)定(ding)義(yi)路(lu)由(you)的(de)(de)時候需要在(zai)加一(yi)個js文(wen)件(jian)(jian),四個文(wen)件(jian)(jian)對應(ying)的(de)(de)位置如下圖:
案(an)例(li)內(nei)容:定義一(yi)個(ge)路由組,仿照官方案(an)例(li)里的(de)goods商品列表。
view:
easyadmin-2\app\admin\view\mall\goodsone

index.html文(wen)件內容為:

這里就可(ke)以(yi)看到定義后的(de)子(zi)路由是什么格式
model:
easyadmin-2\app\admin\model

文(wen)件(jian)內容為:

案例里(li)(li)取消了delete_time限(xian)制,這樣(yang)可以看到表單(dan)里(li)(li)的所有數據
cate方(fang)法為外鍵方(fang)法
controller:
easyadmin-2\app\admin\controller\mall

文件內(nei)容(rong)為(wei):
<?php
namespace app\admin\controller\mall;
use app\admin\model\MallGoodsOne;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\Facade\Db;
use think\App;
/**
* Class Goods
* @package app\admin\controller\mall
* @ControllerAnnotation(title="商城商品管理")
*/
class GoodsOne extends AdminController
{
use Curd;
protected $relationSearch = true;
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new MallGoodsOne();
}
/**
* @NodeAnotation(title="列表")
*/
public function index()
{
//var_dump($this->request->isAjax());exit();
if ($this->request->isAjax()) {
if (input('selectFields')) {
return $this->selectList();
}
list($page, $limit, $where) = $this->buildTableParames();
$count = $this->model
->withJoin('cate', 'LEFT')
->where($where)
->count();
$list = $this->model
->withJoin('cate', 'LEFT')
->where($where)
->page($page, $limit)
->order($this->sort)
->select();
$data = [
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $list,
];
return json($data);
}
return $this->fetch();
}
}
js:
easyadmin-2\public\static\admin\js\mall

內容為:
define(["jquery", "easy-admin"], function ($, ea) {
var init = {
table_elem: '#currentTable',
table_render_id: 'currentTableRenderId',
index_url: 'mall.goodsone/index',
add_url: 'mall.goodsone/add',
edit_url: 'mall.goodsone/edit',
delete_url: 'mall.goodsone/delete',
export_url: 'mall.goodsone/export',
modify_url: 'mall.goodsone/modify',
stock_url: 'mall.goodsone/stock',
};
var Controller = {
index: function () {
ea.table.render({
init: init,
toolbar: ['refresh',
[{
text: '添加',
url: init.add_url,
method: 'open',
auth: 'add',
class: 'layui-btn layui-btn-normal layui-btn-sm',
icon: 'fa fa-plus ',
extend: 'data-full="true"',
}],
'delete', 'export'],
cols: [[
{type: "checkbox"},
{field: 'id', width: 80, title: 'ID'},
{field: 'sort', width: 80, title: '排序', edit: 'text'},
{field: 'cate.title', minWidth: 80, title: '商品分類'},
{field: 'title', minWidth: 80, title: '商品名稱'},
{field: 'logo', minWidth: 80, title: '分類圖片', search: false, templet: ea.table.image},
{field: 'market_price', width: 100, title: '市場價', templet: ea.table.price},
{field: 'discount_price', width: 100, title: '折扣價', templet: ea.table.price},
{field: 'total_stock', width: 100, title: '庫存統計'},
{field: 'stock', width: 100, title: '剩余庫存'},
{field: 'virtual_sales', width: 100, title: '虛擬銷量'},
{field: 'sales', width: 80, title: '銷量'},
{field: 'status', title: '狀態', width: 85, search: 'select',selectList: {0: '禁用', 1: '啟用'}, templet: ea.table.switch},
{field: 'create_time', minWidth: 80, title: '創建時間'},
{
width: 250,
title: '操作',
templet: ea.table.tool,
operat: [
[{
text: '編輯',
url: init.edit_url,
method: 'open',
auth: 'edit',
class: 'layui-btn layui-btn-xs layui-btn-success',
extend: 'data-full="true"',
},
// {
// text: '入庫',
// url: init.stock_url,
// method: 'open',
// auth: 'stock',
// class: 'layui-btn layui-btn-xs layui-btn-normal',
// }
],
'delete']
}
]],
});
ea.listen();
},
add: function () {
ea.listen();
},
edit: function () {
ea.listen();
},
stock: function () {
ea.listen();
},
};
return Controller;
});
還需要在數據庫中創建一個表,表名為goods_one

格式可以參照(zhao)官網案例(li)的(de)goods
以上就是EasyAdmin定義路由的過程,如果你需要查看報錯,你需要修改以下文件
easyadmin-2\config

內(nei)容為:
<?php
// +----------------------------------------------------------------------
// | 應用設置
// +----------------------------------------------------------------------
use think\facade\Env;
return [
// 應用地址
'app_host' => Env::get('app.host', ''),
// 應用的命名空間
'app_namespace' => '',
// 是否啟用路由
'with_route' => true,
// 是否啟用事件
'with_event' => true,
// 開啟應用快速訪問
'app_express' => true,
// 默認應用
'default_app' => 'index',
// 默認時區
'default_timezone' => 'Asia/Shanghai',
// 應用映射(自動多應用模式有效)
'app_map' => [
Env::get('easyadmin.admin', 'admin') => 'admin',
],
// 后臺別名
'admin_alias_name' => Env::get('easyadmin.admin', 'admin'),
// 域名綁定(自動多應用模式有效)
'domain_bind' => [],
// 禁止URL訪問的應用列表(自動多應用模式有效)
'deny_app_list' => ['common'],
// 異常頁面的模板文件
// 'exception_tmpl' => Env::get('app_debug') == 1 ? app()->getThinkPath() . 'tpl/think_exception.tpl' : app()->getBasePath() . 'common' . DIRECTORY_SEPARATOR . 'tpl' . DIRECTORY_SEPARATOR . 'think_exception.tpl',
// 跳轉頁面的成功模板文件
'dispatch_success_tmpl' => app()->getBasePath() . 'common' . DIRECTORY_SEPARATOR . 'tpl' . DIRECTORY_SEPARATOR . 'dispatch_jump.tpl',
// 跳轉頁面的失敗模板文件
'dispatch_error_tmpl' => app()->getBasePath() . 'common' . DIRECTORY_SEPARATOR . 'tpl' . DIRECTORY_SEPARATOR . 'dispatch_jump.tpl',
// 錯誤顯示信息,非調試模式有效
// 'error_message' => '頁面錯誤!請稍后再試~',
// 顯示錯誤信息
'show_error_msg' => true,
// 靜態資源上傳到OSS前綴
'oss_static_prefix' => Env::get('easyadmin.oss_static_prefix', 'static_easyadmin'),
];
如果本文對你有所幫助,麻煩你點個贊,下一章講下如何創建一個表單并進行增刪查改。
