Home > PHP Framework > ThinkPHP > About the current controller and method names cannot be obtained in TP6 multi-application mode

About the current controller and method names cannot be obtained in TP6 multi-application mode

藏色散人
Release: 2020-07-04 14:16:04
forward
5757 people have browsed it

下面由thinkphp框架教程栏目给大家介绍关于TP6多应用模式下获取不到当前控制器和方法名的问题,希望对需要的朋友有所帮助!

前言:最近使用TP6做了一套项目,发现多应用模式下使用 $this->request->controller()$this->request->action() 无法获取到当前的控制器和方法名,自己研究了一下,找了个笨办法,记录一下,如果大家有更好的办法,欢迎留言。

开发环境

windwos 10
PHP 7.3
TP 6.0.2
Copy after login

问题重现

1、先创建一个新项目

composer create-project topthink/think tp60
cd tp60/
composer require topthink/think-multi-app
Copy after login

2、修改 /config/app.php 加入下面两行

//开启应用快速访问
'app_express'      => true,
Copy after login

3、修改 /config/route.php

// 是否强制使用路由
'url_route_must' => true,
// 路由是否完全匹配
'route_complete_match' => true,
Copy after login

4、删除 /app 下面的 controller 目录,创建 index 文件夹,目录结构如下:

About the current controller and method names cannot be obtained in TP6 multi-application mode

5、上代码,IndexController.php 的内容:

<?php

namespace app\index\controller;

use app\BaseController;

class IndexController extends BaseController
{
    public function index()
    {
        dd($this->request->controller(), $this->request->action());
    }
}
Copy after login

/app/index/route/app.php 的内容

<?php

use think\facade\Route;

Route::group(function () {
    Route::get(&#39;/&#39;, &#39;IndexController@index&#39;);
})->prefix(&#39;\app\index\controller\\&#39;);
Copy after login

6、启动然后访问该应用,控制器与方法输出都是空。

php think run
Copy after login

About the current controller and method names cannot be obtained in TP6 multi-application mode

解决方案

调试发现 $this->request 对象的 rule 里面有当前控制器和方法名

About the current controller and method names cannot be obtained in TP6 multi-application mode

可使用 $this->request->rule()->getName()$this->request->rule()->getRoute() 获取,

About the current controller and method names cannot be obtained in TP6 multi-application mode

谜之操作

另外调试发现,Controllerinit 方法好像没执行,在 Request 里面打两个断点,一样可以访问,可以用上面的方法获取到控制器和方法名。
About the current controller and method names cannot be obtained in TP6 multi-application mode

About the current controller and method names cannot be obtained in TP6 multi-application mode


The above is the detailed content of About the current controller and method names cannot be obtained in TP6 multi-application mode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template