reture view();可否默认调用admin/view/login/index.html模板

建站管家

问题描述

比如:admin/controller/Login 的 index 方法,如果模板路径没有特殊要求的话,

webman控制器里:reture view();

那模板文件就是:admin/view/login/index.html

135 2 0
2个回答

梦想世界
 protected function renderView($viewName,$array = []): Response
    {
        $str = request()->controller;
        $prefix = "app\admin\controller\\";
        $controller = strtolower(str_replace($prefix, "", $str));//获取当前请求控制器
        $output = str_replace("\\", "/", $controller);
        // 拼接视图路径
        return view("$output/$viewName",$array);
    }

     /**
     * @title 列表
     */
    public function index(): Response
    {
        if (request()->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParam();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            return $this->success($list,$count,'请求成功',0);
        } else {
            return $this->renderView('index');
        }
    }

我这边是这样做的,只用传方法名称,其实这一步也可以不要,默认当前方法名就行了,我是只有admin应用下才会需要模板,你可以考虑根据自己需求优化一下

  • 暂无评论
nitron

function.php里自己加个新方法

无参数时调用request()获取控制器和方法名称然后自己匹配模板路径
再调用之前的view(模板路径)来输出

但是这么做,跟request强耦合

  • 暂无评论
×
🔝