安装
composer create-project topthink/think .
安装多应用组件
composer require topthink/think-multi-app composer require topthink/think-view
遇到问题#1,
想把应用目录和vendor 放到不同的文件夹中。
App 的 thinkPath / rootPath / appPath / runtimePath 。 需要重新想一想了.....
在6.0.7 版本中,有不少代码里,用的是 app()->getRootPah() . 'app' 或者 app()->getRootPath() . 'vendor' 这种方法,所以没有办法直接解决。
解决问题#1
下面的方法,应该是可以解决绝大部分问题,还在持续观察中。
在 App.php 中,加入 $appRootPath
public function __construct(string $rootPath = '', string $appRootPath = '') // 增加 $appRootPath
{
$this->thinkPath = dirname(__DIR__) . DIRECTORY_SEPARATOR;
$this->rootPath = $rootPath ? rtrim($rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $this->getDefaultRootPath();
$this->appRootPath = empty($appRootPath) ? $this->rootPath : $appRootPath . DIRECTORY_SEPARATOR; // 增加的部分
$this->appPath = $this->appRootPath . 'app' . DIRECTORY_SEPARATOR; // 修改的部分
$this->runtimePath = $this->appRootPath . 'runtime' . DIRECTORY_SEPARATOR; // 修改的部分
// ......... Other codes
}
public function getAppRootPath(): string // 增加的方法
{
return $this->appRootPath;
}
在新入口文件中,增加代码
namespace think;
$loader = include_once '/thinkphp/v6/vendor/autoload.php';
$loader->setPsr4('app\\', [__DIR__.'\\app\\']); // 重写classloader时 app 目录所在目录。
// 执行HTTP应用并响应
$app = new App('', __DIR__); // 增加 $appRootPath
$http = ($app)->http;
$response = $http->run();
$response->send();
$http->end($response);
在部分使用 app 的目录部分,把 app()-> getRootPah() 改成 app()->getAppRootPath()。这里,我只composer 了 think-multi-app / think-view 所以只包括下面:
- /vendor/topthink/framework/src/helper.php (line 635)
- /vendor/topthink/framework/src/Http.php (line 61 / 224)
- /vendor/topthink/framework/src/think/console/command/Clear.php (line 34)
- /vendor/topthink/framework/src/think/console/command/RouteList.php (line 45 / 63 / 65)
- /vendor/topthink/framework/src/think/console/command/RunServer.php (line 55)
- /vendor/topthink/framework/src/think/console/command/optimize/Route.php (line 32 / 49)
- /vendor/topthink/framework/src/think/view/driver/Php.php (line 131)
- /vendor/topthink/think-multi-app/src/command/Clear.php (line 35)
解决问题#2
在使用vue element-plus-admin + tp6 时,验证码部分是跨域的,浏览器不会传递Cookie,造成session无法使用。
- 在中间件中增加,AllowCrossDomain 和 SeesionInit
return [
\think\middleware\AllowCrossDomain::class, // 兼容跨域
\think\middleware\SessionInit::class // 验证码需要启用session
];
2. 使用 request 来代替cookie,来传递session_id。
在config\session.php 中修改 var_session_id 的值到 uuid。
'var_session_id' => 'uuid',
由于这里 uuid 的格式为 xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,需要修改 think\session\Store::setId 方法为
$this->id = is_string($id) && ((strlen($id) === 32 && ctype_alnum($id)) || \preg_match('/[a-z0-9]{8}(-[a-z0-9]{4}){3}-[a-z0-9]{12}/', $id, $mat)) ? $id : md5(microtime(true) . session_create_id());
PS:这里,应该也可以在中间件中先行加密,但原因这里的 request 是没办法修改的,暂时还没找到一个合适的方法,就直接修改 seesion_id 允许的格式了。
© 著作权归作者所有