我正在从快速路由中间件创建路由器。// routes.phpreturn [ ['GET', '/1', ['ShowMe','demo']], ['GET', '/2', ShowMe::class],];如果我在 simpleDispatcher 的函数中手动包含它,一切正常 -两条路线都在工作: // $r->addRoute('GET', '/ddd', ['ShowMe','demo']); // $r->addRoute('GET', '/ddd', ShowMe::class);但是,如果我想从 routes.php 加载路由,我将使用这样的代码:foreach ($routes as $key => $route) { $r->addRoute($route[0], $route[1], [$route[2][0], $route[2][1]]); }这项工作非常适合这样调用函数的路线:['GET', '/1', ['ShowMe','demo']],所以我修改(因为我有 2 种路由类型并且希望这个语法支持它们): $r->addRoute($route[0], $route[1], [implode(",", $route[2])]);它现在在两种路线的类型中都不起作用:Warning: implode(): Invalid arguments passed in ...\index.php on line 69Fatal error: Uncaught RuntimeException: Invalid request handler: array
1 回答
米脂
TA贡献1836条经验 获得超3个赞
你的foreach循环语法有错误语法是foreach($arrayName as $key => $value)
foreach ($routes as $key => $route) {
$r->addRoute($routes[$key]); # or simply $r->addRoute($route);
}
其次,该implode()函数将数组作为字符串返回,我猜你不想要。所以简单地说:
$r->addRoute($route[0], $route[1], $route[2]);
- 1 回答
- 0 关注
- 194 浏览
添加回答
举报
0/150
提交
取消