经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
TP6框架--EasyAdmin学习笔记:定义路由
来源:cnblogs  作者:林恒  时间:2021/5/24 10:57:51  对本文有异议

这是我写的学习EasyAdmin的第二章,这一章我给大家分享下如何定义一条路由

正常的tp6定义路由方法如下:

 /route/admins/app.php 文件内容

  1. //路由变量自定义
  2. Route::get('zhanghao/denglu/[:aaa]','app\\admins\\controller\\Account@login');

\app\admins\controller\Account.php 文件内容

  1. <?php
  2. namespace app\admins\controller;
  3. use think\facade\Db;
  4. use app\BaseController;
  5. class Account extends BaseController{
  6.  
  7. public function login($aaa){
  8. return $aaa;
  9. return view();
  10. }
  11.  
  12. }

以上是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. <?php
  2.  
  3.  
  4. namespace app\admin\controller\mall;
  5.  
  6.  
  7. use app\admin\model\MallGoodsOne;
  8. use app\admin\traits\Curd;
  9. use app\common\controller\AdminController;
  10. use EasyAdmin\annotation\ControllerAnnotation;
  11. use EasyAdmin\annotation\NodeAnotation;
  12. use think\Facade\Db;
  13. use think\App;
  14.  
  15. /**
  16. * Class Goods
  17. * @package app\admin\controller\mall
  18. * @ControllerAnnotation(title="商城商品管理")
  19. */
  20. class GoodsOne extends AdminController
  21. {
  22.  
  23. use Curd;
  24.  
  25. protected $relationSearch = true;
  26.  
  27. public function __construct(App $app)
  28. {
  29. parent::__construct($app);
  30. $this->model = new MallGoodsOne();
  31. }
  32.  
  33. /**
  34. * @NodeAnotation(title="列表")
  35. */
  36. public function index()
  37. {
  38. //var_dump($this->request->isAjax());exit();
  39. if ($this->request->isAjax()) {
  40. if (input('selectFields')) {
  41. return $this->selectList();
  42. }
  43. list($page, $limit, $where) = $this->buildTableParames();
  44. $count = $this->model
  45. ->withJoin('cate', 'LEFT')
  46. ->where($where)
  47. ->count();
  48. $list = $this->model
  49. ->withJoin('cate', 'LEFT')
  50. ->where($where)
  51. ->page($page, $limit)
  52. ->order($this->sort)
  53. ->select();
  54. $data = [
  55. 'code' => 0,
  56. 'msg' => '',
  57. 'count' => $count,
  58. 'data' => $list,
  59. ];
  60. return json($data);
  61. }
  62. return $this->fetch();
  63. }
  64. }

js:

easyadmin-2\public\static\admin\js\mall

 内容为:

  1. define(["jquery", "easy-admin"], function ($, ea) {
  2.  
  3. var init = {
  4. table_elem: '#currentTable',
  5. table_render_id: 'currentTableRenderId',
  6. index_url: 'mall.goodsone/index',
  7. add_url: 'mall.goodsone/add',
  8. edit_url: 'mall.goodsone/edit',
  9. delete_url: 'mall.goodsone/delete',
  10. export_url: 'mall.goodsone/export',
  11. modify_url: 'mall.goodsone/modify',
  12. stock_url: 'mall.goodsone/stock',
  13. };
  14.  
  15. var Controller = {
  16.  
  17. index: function () {
  18. ea.table.render({
  19. init: init,
  20. toolbar: ['refresh',
  21. [{
  22. text: '添加',
  23. url: init.add_url,
  24. method: 'open',
  25. auth: 'add',
  26. class: 'layui-btn layui-btn-normal layui-btn-sm',
  27. icon: 'fa fa-plus ',
  28. extend: 'data-full="true"',
  29. }],
  30. 'delete', 'export'],
  31. cols: [[
  32. {type: "checkbox"},
  33. {field: 'id', width: 80, title: 'ID'},
  34. {field: 'sort', width: 80, title: '排序', edit: 'text'},
  35. {field: 'cate.title', minWidth: 80, title: '商品分类'},
  36. {field: 'title', minWidth: 80, title: '商品名称'},
  37. {field: 'logo', minWidth: 80, title: '分类图片', search: false, templet: ea.table.image},
  38. {field: 'market_price', width: 100, title: '市场价', templet: ea.table.price},
  39. {field: 'discount_price', width: 100, title: '折扣价', templet: ea.table.price},
  40. {field: 'total_stock', width: 100, title: '库存统计'},
  41. {field: 'stock', width: 100, title: '剩余库存'},
  42. {field: 'virtual_sales', width: 100, title: '虚拟销量'},
  43. {field: 'sales', width: 80, title: '销量'},
  44. {field: 'status', title: '状态', width: 85, search: 'select',selectList: {0: '禁用', 1: '启用'}, templet: ea.table.switch},
  45. {field: 'create_time', minWidth: 80, title: '创建时间'},
  46. {
  47. width: 250,
  48. title: '操作',
  49. templet: ea.table.tool,
  50. operat: [
  51. [{
  52. text: '编辑',
  53. url: init.edit_url,
  54. method: 'open',
  55. auth: 'edit',
  56. class: 'layui-btn layui-btn-xs layui-btn-success',
  57. extend: 'data-full="true"',
  58. },
  59. // {
  60. // text: '入库',
  61. // url: init.stock_url,
  62. // method: 'open',
  63. // auth: 'stock',
  64. // class: 'layui-btn layui-btn-xs layui-btn-normal',
  65. // }
  66. ],
  67. 'delete']
  68. }
  69. ]],
  70. });
  71.  
  72. ea.listen();
  73. },
  74. add: function () {
  75. ea.listen();
  76. },
  77. edit: function () {
  78. ea.listen();
  79. },
  80. stock: function () {
  81. ea.listen();
  82. },
  83. };
  84. return Controller;
  85. });

还需要在数据库中创建一个表,表名为goods_one

 格式可以参照官网案例的goods

以上就是EasyAdmin定义路由的过程,如果你需要查看报错,你需要修改以下文件

easyadmin-2\config

 内容为:

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 应用设置
  4. // +----------------------------------------------------------------------
  5.  
  6. use think\facade\Env;
  7.  
  8. return [
  9. // 应用地址
  10. 'app_host' => Env::get('app.host', ''),
  11. // 应用的命名空间
  12. 'app_namespace' => '',
  13. // 是否启用路由
  14. 'with_route' => true,
  15. // 是否启用事件
  16. 'with_event' => true,
  17. // 开启应用快速访问
  18. 'app_express' => true,
  19. // 默认应用
  20. 'default_app' => 'index',
  21. // 默认时区
  22. 'default_timezone' => 'Asia/Shanghai',
  23. // 应用映射(自动多应用模式有效)
  24. 'app_map' => [
  25. Env::get('easyadmin.admin', 'admin') => 'admin',
  26. ],
  27. // 后台别名
  28. 'admin_alias_name' => Env::get('easyadmin.admin', 'admin'),
  29. // 域名绑定(自动多应用模式有效)
  30. 'domain_bind' => [],
  31. // 禁止URL访问的应用列表(自动多应用模式有效)
  32. 'deny_app_list' => ['common'],
  33. // 异常页面的模板文件
  34. // 'exception_tmpl' => Env::get('app_debug') == 1 ? app()->getThinkPath() . 'tpl/think_exception.tpl' : app()->getBasePath() . 'common' . DIRECTORY_SEPARATOR . 'tpl' . DIRECTORY_SEPARATOR . 'think_exception.tpl',
  35. // 跳转页面的成功模板文件
  36. 'dispatch_success_tmpl' => app()->getBasePath() . 'common' . DIRECTORY_SEPARATOR . 'tpl' . DIRECTORY_SEPARATOR . 'dispatch_jump.tpl',
  37. // 跳转页面的失败模板文件
  38. 'dispatch_error_tmpl' => app()->getBasePath() . 'common' . DIRECTORY_SEPARATOR . 'tpl' . DIRECTORY_SEPARATOR . 'dispatch_jump.tpl',
  39. // 错误显示信息,非调试模式有效
  40. // 'error_message' => '页面错误!请稍后再试~',
  41. // 显示错误信息
  42. 'show_error_msg' => true,
  43. // 静态资源上传到OSS前缀
  44. 'oss_static_prefix' => Env::get('easyadmin.oss_static_prefix', 'static_easyadmin'),
  45. ];

如果本文对你有所帮助,麻烦你点个赞,下一章讲下如何创建一个表单并进行增删查改。

 

原文链接:http://www.cnblogs.com/smileZAZ/p/14777596.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号