admin管理员组

文章数量:1633770

目的:

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

遇到的问题:

 在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);

本文标签: 控制器入口文件