TP6框架--EasyAdmin学习笔记:定义路由
这是我写的学习EasyAdmin的第二章,这一章我给大家分享下如何定义一条路由
正常的tp6定义路由方法如下:
/route/admins/app.php 文件内容
1 2 | //路由变量自定义 Route:: get ( 'zhanghao/denglu/[:aaa]' , 'app\\admins\\controller\\Account@login' ); |
\app\admins\controller\Account.php 文件内容
1 2 3 4 5 6 7 8 9 10 11 12 | <?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
文件内容为:
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 | <?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
内容为:
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 | 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
内容为:
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 | <?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' ), ]; |
如果本文对你有所帮助,麻烦你点个赞,下一章讲下如何创建一个表单并进行增删查改。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升