简体   繁体   English

Slim 4 - 未捕获的类型错误:传递给 {closure}() 的参数 1 必须是 RouterCollectorProxy 的实例

[英]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:编译器需要知道类的全限定名,所以有两个选项:

  1. use fully qualified name of the class when type hinting (not recommended):类型提示时使用类的完全限定名称(不推荐):

     $app->group('/api/v1', function( Slim\\Routing\\RouteCollectorProxy $group){});
  2. 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.

相关问题 未捕获的TypeError:传递给…的参数1必须是mysqli的实例,给定为null - Uncaught TypeError: Argument 1 passed to … must be an instance of mysqli, null given Slim3-传递给构造函数的参数1必须是Slim \\ Views \\ Twig的实例 - Slim3 - Argument 1 passed to constructor must be instance of Slim\Views\Twig 致命错误:未捕获的TypeError:传递给DOMNode :: appendChild()的参数1必须是DOMNode的实例,字符串在 - Fatal error: Uncaught TypeError: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, string given in PHP 7 致命错误:未捕获的 TypeError:传递给 Some2::send() 的参数 1 必须是 Path\To 的实例,Path\To\Some 的实例 - PHP 7 Fatal error: Uncaught TypeError: Argument 1 passed to Some2::send() must be an instance of Path\To, instance of Path\To\Some given 未捕获的类型错误:传递给 Mage_Core_Model_Store_Group::setWebsite() 的参数 1 必须是 Mage_Core_Model_Website 的实例,给定为 null - Uncaught TypeError: Argument 1 passed to Mage_Core_Model_Store_Group::setWebsite() must be an instance of Mage_Core_Model_Website, null given Laravel 8:传递的参数 2 必须是 - Laravel 8: Argument 2 passed must be an instance of 传递给 Darryldecode\\Cart\\Cart::Darryldecode\\Cart\\{closure}() 的参数 1 必须是 Darryldecode\\Cart\\CartCondition 的实例 - Argument 1 passed to Darryldecode\Cart\Cart::Darryldecode\Cart\{closure}() must be an instance of Darryldecode\Cart\CartCondition 传递给__construct()的参数1必须是GuzzleHttp \\ Client的实例 - Argument 1 passed to __construct() must be an instance of GuzzleHttp\Client 传递给:: __ construct()的参数1必须是DateTimeInterface的实例 - Argument 1 passed to ::__construct() must be an instance of DateTimeInterface 传递给的参数1必须是int的实例,给定整数 - Argument 1 passed to must be an instance of int, integer given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM