Workerman无内置路由,需手动解析URL并匹配处理函数。通过$_SERVER['REQUEST_URI']获取URL,用parse_url()解析路径与参数,结合switch、路由表或正则表达式实现路由逻辑。可配置路由规则、支持参数提取,如/users/{id}通过preg_match匹配。为提升效率,可采用路由缓存、路由树或优先常用规则。静态资源通过/static路径映射到public目录直接返回文件内容,实现高效访问。

Workerman本身并不像传统框架那样内置复杂的路由系统。它的路由实现依赖于开发者自行编写逻辑来解析URL,并根据URL的不同部分执行相应的处理函数。简而言之,你需要自己“动手”实现路由功能。
解决方案
在Workerman中实现路由,核心在于接收客户端请求的URL,然后根据URL的不同部分(例如路径、查询参数)来决定执行哪个函数或类的方法。
接收URL: 在你的Worker进程中,你需要监听客户端的请求。当接收到请求时,可以从
$_SERVER['REQUEST_URI']
解析URL: 使用
parse_url()
路由匹配: 编写逻辑来匹配不同的URL路径到相应的处理函数。这可以用
switch
if-else
执行处理函数: 根据路由匹配的结果,调用相应的处理函数来生成响应并发送给客户端。
下面是一个简单的示例,展示了如何使用
switch
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/Autoloader.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, $data) {
$url = $_SERVER['REQUEST_URI'];
$parsedUrl = parse_url($url);
$path = $parsedUrl['path'] ?? '/'; // 默认路径
switch ($path) {
case '/':
$connection->send('Welcome to the homepage!');
break;
case '/about':
$connection->send('About us page.');
break;
case '/contact':
$connection->send('Contact us page.');
break;
default:
$connection->send('404 Not Found');
}
};
$worker->runAll();这个例子非常基础,但它展示了核心思想。你可以根据自己的需求扩展这个示例,例如:
Workerman URL路由配置?
严格来说,Workerman本身并没有提供专门的“URL路由配置”功能。路由的实现完全取决于你的代码逻辑。不过,你可以将路由规则配置化,例如将路由规则存储在配置文件中,然后在代码中读取这些配置,从而实现更灵活的路由管理。
当URL结构变得复杂,并且需要传递参数时,简单的
switch
preg_match()
/users/{id}{id}以下是一个使用正则表达式路由的示例:
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/Autoloader.php';
$worker = new Worker('http://0.0.0.0:8080');
$routes = [
'#^/$#' => 'homeHandler',
'#^/users/(\d+)$#' => 'userHandler', // 匹配 /users/123
];
$worker->onMessage = function(TcpConnection $connection, $data) use ($routes) {
$url = $_SERVER['REQUEST_URI'];
foreach ($routes as $pattern => $handler) {
if (preg_match($pattern, $url, $matches)) {
// 根据handler调用相应的函数
switch ($handler) {
case 'homeHandler':
homeHandler($connection);
break;
case 'userHandler':
$userId = $matches[1]; // 获取匹配的ID
userHandler($connection, $userId);
break;
}
return; // 找到匹配的路由,结束循环
}
}
$connection->send('404 Not Found');
};
function homeHandler(TcpConnection $connection) {
$connection->send('Home Page');
}
function userHandler(TcpConnection $connection, $userId) {
$connection->send("User ID: " . $userId);
}
$worker->runAll();
当路由规则很多时,顺序匹配可能会变得效率低下。可以考虑以下优化方法:
静态资源(例如CSS、JavaScript、图片)通常不需要路由处理。可以将静态资源放在一个专门的目录中,然后直接通过URL访问。
public
/static
public
/static/style.css
/static/image.jpg
在Workerman中,你可以添加一个特殊的路由规则来处理静态资源请求。如果请求的URL以
/static
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/Autoloader.php';
$worker = new Worker('http://0.0.0.0:8080');
$publicDir = __DIR__ . '/public';
$worker->onMessage = function(TcpConnection $connection, $data) use ($publicDir) {
$url = $_SERVER['REQUEST_URI'];
if (strpos($url, '/static/') === 0) {
$filePath = $publicDir . substr($url, strlen('/static'));
if (file_exists($filePath) && is_file($filePath)) {
$fileContent = file_get_contents($filePath);
$connection->send($fileContent);
return;
}
}
// 其他路由规则...
$connection->send('404 Not Found');
};
$worker->runAll();确保在你的
public
以上就是Workerman如何实现路由功能?WorkermanURL路由配置?的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号