您是否需要用于异步PHP的Composer和像ReactPHP或Swoole这样的HTTP服务器



据我了解,Composer用于通过PHP提供的SPL函数自动加载类,或者至少注册在类不存在时要调用的方法。然后,这必须在每次请求使用Laravel或CakePHP进行传统设置时发生,例如......

我的问题是,在Swoole HTTP服务器的情况下,Composer如何工作,你可以自由地预先加载所有内容?在这种情况下甚至需要作曲家吗?

Swoole HTTP PHP Server的基本术语如下所示:

<?php
// Load all your classes and files here?
$http = new swoole_http_server("127.0.0.1", 9501);
$http->on("start", function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9501n";
});
$http->on("request", function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello Worldn");
});
$http->start();

所以我可以事先加载所有内容,而不必担心必须调用任何自动加载脚本?

然后,所有

类都将在全局范围内,因此,所有内容都已预加载并准备在->on("request")函数回调中使用。

您可以在 CLI 上下文中使用 composer 及其自动加载功能,与 Swoole 一起使用。

PHP 的执行没有变化,所以自动加载器可以正常工作,只需在相关脚本中包含vendor/autoload.php即可。

<?php
// Autoloader is now up, you can use new Your/Class;
require_once('vendor/autoload.php'); 
$http = new swoole_http_server("127.0.0.1", 9501);
$http->on("start", function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9501n";
});
$http->on("request", function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello Worldn");
});
$http->start();

免责声明:我将 swoole 与 Laravel、Lumen 和自定义解决方案(CLI 和 fastcgi(/web 一起使用,它效果很好,并且在该上下文中使用 PHP 的方式没有变化。

最新更新