[英]Slim 4 - Uncaught TypeError: Argument 1 passed to {closure}() must be an instance of RouterCollectorProxy
I'm following the guide of slim 4 to create routes group but I get this error when I try to test the deployed heroku app:我正在按照 slim 4 的指南创建路由组,但是当我尝试测试已部署的 heroku 应用程序时出现此错误:
PHP Fatal error: Uncaught TypeError: Argument 1 passed to {closure}() must be an instance of RouterCollectorProxy, instance of Slim\Routing\RouteCollectorProxy given, called in /app/vendor/slim/slim/Slim/Routing/RouteGroup.php on line 75 and defined in /app/index.php:17
what's wrong with my code?Maybe I've missed something?我的代码有什么问题?也许我错过了什么?
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Updater\DefinitionUpdater;
require __DIR__.'/vendor/autoload.php';
$app = AppFactory::create();
$app->get('/',function(Request $request, Response $response, $args){
$response->getBody()->write('Hello world!');
return $response;
});
$app->group('/api/v1', function(RouteCollectorProxy $group){
/**/
$app->get('/list', function(Request $request, Response $response, $args){
$list = DefinitionUpdater::updateList();
$response->getBody()->write( json_encode($list, JSON_UNESCAPED_SLASHES) );
return $response->withHeader( 'Content-Type', 'application/json' );
})->setName('privacy');
/**/
$app->get('/privacy', function(Request $request, Response $response, $args){
$privacy = DefinitionUpdater::updatePrivacy();
$response->getBody()->write( json_encode($privacy, JSON_UNESCAPED_SLASHES) );
return $response->withHeader( 'Content-Type', 'application/json' );
})->setName('privacy');
/**/
$app->get('/cookie', function(Request $request, Response $response, $args){
$cookie = DefinitionUpdater::updateCookie();
$response->getBody()->write( json_encode($cookie, JSON_UNESCAPED_SLASHES) );
return $response->withHeader( 'Content-Type', 'application/json' );
})->setName('cookie');
});
$app->run();
$app->addErrorMiddleware(true, true, true);
?>
You type hinted $group
as RouteCollectorProxy
.您键入
RouteCollectorProxy
$group
作为RouteCollectorProxy
。 The compiler needs to know about the fully qualified name of the class, so there are two options:编译器需要知道类的全限定名,所以有两个选项:
use fully qualified name of the class when type hinting (not recommended):类型提示时使用类的完全限定名称(不推荐):
$app->group('/api/v1', function( Slim\\Routing\\RouteCollectorProxy $group){});
add another use statement:添加另一个使用语句:
use Slim\\Routing\\RouteCollectorProxy;
There is also another problem with your code.您的代码还有另一个问题。 In route group callback, you should use
$group
to add actual routes:在路由组回调中,您应该使用
$group
添加实际路由:
use Slim\Routing\RouteCollectorProxy;
$app->group('/api/v1', function(RouteCollectorProxy $group){
$group->get('/list', function(Request $request, Response $response, $args){
//
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.