[英]Define global constant with mezzio swoole/openswoole
im trying to set a global constant that would be accessible to all php swoole processes and threads later, but it seems to be not visible or something我试图设置一个全局常量,以后所有 php swoole 进程和线程都可以访问它,但它似乎不可见或其他东西
Here's my index.php - almost vanilla mezzio - I only see APPROOT constant:这是我的 index.php - 几乎是香草 mezzio - 我只看到APPROOT常量:
(function () {
if (!defined('APPROOT')) {
define('APPROOT', __DIR__);
}
/** @var \Psr\Container\ContainerInterface $container */
$container = require 'config/container.php';
/** @var \Mezzio\Application $app */
$app = $container->get(\Mezzio\Application::class);
$factory = $container->get(\Mezzio\MiddlewareFactory::class);
// Execute programmatic/declarative middleware pipeline and routing
// configuration statements
(require 'config/pipeline.php')($app, $factory, $container);
(require 'config/routes.php')($app, $factory, $container);
$app->run();
})();
But then I get an error when I try to reference/use that constant in one of the middlewares:但是当我尝试在其中一个中间件中引用/使用该常量时出现错误:
<?php
declare(strict_types=1);
namespace Application\Middleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class BootstrapMiddleware implements MiddlewareInterface
{
public function __construct(private ResponseFactoryInterface $responseFactory)
{
}
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
$this->setAssetsCompiledLoc();
$response = $handler->handle($request);
return $response;
}
private function setAssetsCompiledLoc()
{
if ( ! defined('ASSET_MAP')) {
$manifestPath = \APPROOT . '/manifests/manifest-' . ENV . '.json';
// ...blah...
}
}
}
I get this error:我收到此错误:
Error Undefined constant "APPROOT"
错误未定义常量“APPROOT”
Why?为什么? Is that because all variable outside of the swoole context are discarded and I set this outside of swoole context?
那是因为 swoole 上下文之外的所有变量都被丢弃了,而我在 swoole 上下文之外设置了这个?
Upon reviewing source code for Mezzio\Swoole\Command\StartCommand I've discovered that index.php that I thought was an entry point to my php swoole application is actually not being used at all.在查看 Mezzio\Swoole\Command\StartCommand 的源代码后,我发现我认为是我的 php swoole 应用程序的入口点的 index.php 实际上根本没有被使用。
thus constants were indeed undefined.因此常量确实是未定义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.