简体   繁体   English

Slim3使用引用是错误的

[英]Slim3 use reference is wrong

I'm tring to prepare a simple empty application with Slim3 and composer. 我想用Slim3和作曲家准备一个简单的空应用程序。

This is my front controller: 这是我的前端控制器:

<?php

require '../vendor/autoload.php';


    $config=[];
    $config['displayErrorDetails'] = true;
    $config['addContentLengthHeader'] = false;
    $app = new \Slim\App(['settings' => $config]);

    foreach(glob("../app/dependencies/*.php") as $dependency){
        $dependency=include $dependency;
        $dependency($app);
    }
    foreach(glob("../app/middleware/*.php") as $middleware){
        $middleware=include $middleware;
        $middleware($app);
    }
    foreach(glob("../app/routes/*.php") as $route){
        $route=include $route;
        $route($app);
    }

    $app->run();

And this is the unique route file I have: 这是我拥有的唯一路径文件:

<?php 

return function (\Slim\App $app){
    $app->get('/', function (Request $request, Response $response) {
        $response->getBody()->write("Hello");
        return $response;
    });
};

When I run the application I got an error: 当我运行应用程序时,我收到一个错误:

Argument 1 passed to Closure::{closure}() must be an instance of Request, instance of Slim\\Http\\Request given 传递给Closure :: {closure}()的参数1必须是Request的实例,给出了Slim \\ Http \\ Request的实例

I must add this "uses" to my route file: 我必须将此“使用”添加到我的路径文件中:

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

Why php is taking the wrong (Slim) class for request and response? 为什么php为请求和响应采取错误(Slim)类? Really I have to prefix them for each controller file? 真的我必须为每个控制器文件添加前缀吗?

Really I have to prefix them for each controller file? 真的我必须为每个控制器文件添加前缀吗?

Yes. 是。 use declarations are per file as documented in the PHP manual : use声明是每个文件,如PHP手册中所述

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules. 导入规则是基于每个文件的,这意味着包含的文件不会继承父文件的导入规则。


You might or might not be able to leave out the type in front of the parameter. 您可能会或可能不会在参数前面省略类型。 Whether you are able to do so depends on whether Slim uses Reflection to determine whether to put the $request and $response object. 是否能够这样做取决于Slim是否使用Reflection来确定是否放置$request$response对象。 Based off the error message you get I suppose you are able to simply remove the type. 基于您收到的错误消息,我想您可以简单地删除该类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM