這是我寫的學習EasyAdmin的第二章,這一章我給大家分享下如何定義一條路由
正常的tp6定義路由方法如下:
/route/admins/app.php 文件內容
//路由變量自定義 Route::get('zhanghao/denglu/[:aaa]','app\\admins\\controller\\Account@login');
\app\admins\controller\Account.php 文件內容
<?php namespace app\admins\controller; use think\facade\Db; use app\BaseController; class Account extends BaseController{ public function login($aaa){ return $aaa; return view(); } }
以上是tp6的路由定義流程,我們可以看出,tp6定義路由是view+model+controller三層文件組成的,而EasyAdmin里運用了layui框架,在定義路由的時候需要在加一個js文件,四個文件對應的位置如下圖:
案例內容:定義一個路由組,仿照官方案例里的goods商品列表。
view:
easyadmin-2\app\admin\view\mall\goodsone
index.html文件內容為:
這里就可以看到定義后的子路由是什么格式
model:
easyadmin-2\app\admin\model
文件內容為:
案例里取消了delete_time限制,這樣可以看到表單里的所有數據
cate方法為外鍵方法
controller:
easyadmin-2\app\admin\controller\mall
文件內容為:
<?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
格式可以參照官網案例的goods
以上就是EasyAdmin定義路由的過程,如果你需要查看報錯,你需要修改以下文件
easyadmin-2\config
內容為:
<?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'), ];
如果本文對你有所幫助,麻煩你點個贊,下一章講下如何創建一個表單並進行增刪查改。