thinkphp6 入口文件直接访问控制器

目的:

运行程序前,检测是否已安装了项目,没有安装就跳转到安转页面进行安装。

遇到的问题:

 在config.php中 with_route=>true 无法直接使用$request->setController('Index');或$request->setAction('index')设置当前访问的控制器和方法

在设置了request的controller和action属性后且config.php中 with_route=>false就可以跳转到对应控制器的方法】

$app = new App();
$request = $app->request;
$request->setController('Index');
$request->setAction('index');

$http = $app->http;
$response = $http->run($request); //必须传递request对象

$response->send();

$http->end($response);

$withRoute是在dispatchToRoute()方法中直接获取config/app.php的配置的,无法动态修改,除非直接修改app.php

$app = new App();
$config = $app->config;
$config->set(['with_route' => false], 'app');  //这里设置的是类的属性,路由调度获取的是配置

$response = $http->run()----runWithRequest()----dispatchToRoute()----dispatch() 

if ($withRoute) {
    //'with_route'=> true
    //加载路由
    if ($withRoute instanceof Closure) {
        $withRoute();
    }

    $dispatch = $this->check();    //这里后面也会调用$this->path()会处理pathinfo
} else {
    //如果config/app.php中'with_route'=> false,不走路由
    $dispatch = $this->url($this->path());   //$this->path()会处理pathinfo
}

protected function path(): string
{
    $suffix   = $this->config['url_html_suffix'];
    $pathinfo = $this->request->pathinfo();
    if (false === $suffix) {
        // 禁止伪静态访问
        $path = $pathinfo;
    } elseif ($suffix) {
        // 去除正常的URL后缀
        $path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
    } else {
        // 允许任何后缀访问
        $path = preg_replace('/\.' . $this->request->ext() . '$/i', '', $pathinfo);
    }
    return $path;
}  

最终会通过think\route\dispatch\Url中的parseUrl解析url来调用SetController和SetAction

最终只需要设置pathinfo即可

$app = new App();
$request = $app->request;
$request->setPathinfo('/Install/index');  //【设置控制器-只能访问控制器】
//$request->setPathinfo('install');         //【设置为路由,通过路由可以访问任何类】   

$http = $app->http;

$response = $http->run($request);  //必须传递request对象

$response->send();

$http->end($response);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值