多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 升级指南 ***** If you are upgrading from version 3 to version 4, these are the significant changes that you need to be aware of. > 如果您正在从版本3升级到版本4,那么您需要注意以下重要更改。 ## PHP 版本要求 Slim 4 requires**PHP 7.1 or newer**. ## 更改为Slim\App构造函数 Slim’s App settings used to be a part of the container and they have now been decoupled from it. > Slim的应用程序设置曾经是容器的一部分,现在它们已与容器解耦。 ~~~php /** * Slim 3 App::__construct($container = []) * As seen here the settings used to be nested */ $app = new App([ 'settings' => [...], ]); /** * Slim 4 App::__constructor() method takes 1 mandatory parameter and 4 optional parameters * * @param ResponseFactoryInterface Any implementation of a ResponseFactory * @param ContainerInterface|null Any implementation of a Container * @param CallableResolverInterface|null Any implementation of a CallableResolver * @param RouteCollectorInterface|null Any implementation of a RouteCollector * @param RouteResolverInterface|null Any implementation of a RouteResolver */ $app = new App(...); ~~~ ## 删除应用程序设置 * `addContentLengthHeader`See[Content Length Middleware](http://www.slimframework.com/docs/v4/middleware/content-length.html)for new implementation of this setting. * `determineRouteBeforeAppMiddleware`Position[Routing Middleware](http://www.slimframework.com/docs/v4/middleware/routing.html)at the right position in your middleware stack to replicate existing behavior. * `outputBuffering`See[Output Buffering Middleware](http://www.slimframework.com/docs/v4/middleware/output-buffering.html)for new implementation of this setting. * `displayErrorDetails`See[Error Handling Middleware](http://www.slimframework.com/docs/v4/middleware/error-handling.html)for new implementation of this setting. > * `addContentLengthHeader`有关此设置的新实现,请参阅[内容长度中间件](http://www.slimframework.com/docs/v4/middleware/content-length.html)。 > * `determineRouteBeforeAppMiddleware`将[路由中间件](http://www.slimframework.com/docs/v4/middleware/routing.html)放置在中间件堆栈中的正确位置,以复制现有行为。 > * `outputBuffering`有关此设置的新实现,请参见[输出缓冲中间件](http://www.slimframework.com/docs/v4/middleware/output-buffering.html)。 > * `displayErrorDetails`有关此设置的新实现,请参见[错误处理中间件](http://www.slimframework.com/docs/v4/middleware/error-handling.html)。 ## 改变容器 Slim no longer has a Container so you need to supply your own. If you were relying on request or response being in the container, then you need to either set them to a container yourself, or refactor. Also,`App::__call()`method has been removed, so accessing a container property via`$app->key_name()`no longer works. > Slim不再有容器,所以您需要提供自己的容器。如果您依赖于请求或响应在容器中,那么您需要自己将它们设置为容器,或者进行重构。此外,`App:: call() `方法已被删除,因此通过`$ App ->key_name()`访问容器属性不再工作。 ## 对路由组件的更改 The`Router`component from Slim 3 has been split into multiple different components in order to decouple FastRoute from the`App`core and offer more flexibility to the end user. It has been split into`RouteCollector`,`RouteParser`and`RouteResolver`. Those 3 components can all have their respective interfaces which you can implement on your own and inject into the`App`constructor. The following pull requests offer a lot of insight on the public interfaces of these new components: > 来自Slim 3的“Router”组件被分成多个不同的组件,以将FastRoute从“App”核心中分离出来,并为终端用户提供更多的灵活性。它被分为“RouteCollector”、“RouteParser”和“RouteResolver”。这三个组件都有各自的接口,你可以自己实现并注入到“App”构造函数中。下面的拉请求提供了关于这些新组件的公共接口的大量信息: * [Pull Request #2604](https://github.com/slimphp/Slim/pull/2604) * [Pull Request #2622](https://github.com/slimphp/Slim/pull/2622) * [Pull Request #2639](https://github.com/slimphp/Slim/pull/2639) * [Pull Request #2640](https://github.com/slimphp/Slim/pull/2640) * [Pull Request #2641](https://github.com/slimphp/Slim/pull/2641) * [Pull Request #2642](https://github.com/slimphp/Slim/pull/2642) ## 新的中间件的方法 In Slim 4 we wanted to give more flexibility to the developers by decoupling some of Slim’s App core functionality and implementing it as middleware. This gives you the ability to swap in custom implementations of the core components. > 在Slim 4中,我们希望通过解耦Slim的一些核心功能并将其作为中间件实现,从而为开发人员提供更大的灵活性。这使您能够交换核心组件的自定义实现。 ## 执行中间件 Middleware execution has not changed and is still`Last In First Out (LIFO)`like in Slim 3. > 中间件执行没有改变,仍然是`Last In First Out (LIFO)`,就像在Slim 3中一样。 ## 新的应用工厂 The`AppFactory`component was introduced to reduce some of the friction caused by decoupling the PSR-7 implementation from the`App`core. It detects which PSR-7 implementation and ServerRequest creator is installed in your project root and enables you to instantiate an app via`AppFactory::create()`and use`App::run()`without having to pass in a`ServerRequest`object. The following PSR-7 implementations and ServerRequest creator combos are supported: > 引入`AppFactory`组件是为了减少将PSR-7实现与`App`内核解耦所带来的一些摩擦。它可以检测在您的项目根目录中安装了哪些PSR-7实现和ServerRequest creator,并允许您通过`AppFactory::create()`实例化一个应用程序,并使用`app::run()`,而不必传递一个`ServerRequest`对象。支持以下PSR-7实现和ServerRequest creator组合: * [Slim PSR-7](https://github.com/slimphp/Slim-Psr7) * [Nyholm PSR-7](https://github.com/Nyholm/psr7)and[Nyholm PSR-7 Server](https://github.com/Nyholm/psr7-server) * [Guzzle PSR-7](https://github.com/guzzle/psr7)and[Guzzle HTTP Factory](https://github.com/http-interop/http-factory-guzzle) * [Zend Diactoros](https://github.com/zendframework/zend-diactoros) ## 新的路由中间件 The routing has been implemented as middleware. We are still using[FastRoute](https://github.com/nikic/FastRoute)for our routing needs. If you were using`determineRouteBeforeAppMiddleware`, you need to add the`Middleware\RoutingMiddleware`middleware to your application just before you call`run()`to maintain the previous behaviour. See[Pull Request #2288](https://github.com/slimphp/Slim/pull/2288)for more information. > 路由已作为中间件实现。我们仍然使用[FastRoute](https://github.com/nikic/FastRoute)来满足我们的路由需求。如果您正在使用`determineroutebeforeappmiddleware`,那么您需要在调用`run()`之前将'中间件\路由中间件'中间件添加到您的应用程序中,以维护前面的行为。有关更多信息,请参见[Pull Request #2288](https://github.com/slimphp/Slim/pull/2288)。 ~~~php <?php use Slim\Factory\AppFactory; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); // Add Routing Middleware $app->addRoutingMiddleware(); // ... $app->run(); ~~~ ## 新的错误处理中间件 Error handling has also been implemented as middleware. See[Pull Request #2398](https://github.com/slimphp/Slim/pull/2398)for more information. > 错误处理也作为中间件实现。有关更多信息,请参见[Pull Request #2398](https://github.com/slimphp/Slim/pull/2398)。 ~~~php <?php use Slim\Factory\AppFactory; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); /* * The routing middleware should be added before the ErrorMiddleware * Otherwise exceptions thrown from it will not be handled */ $app->addRoutingMiddleware(); /* * Add Error Handling Middleware * * @param bool $displayErrorDetails -> Should be set to false in production * @param bool $logErrors -> Parameter is passed to the default ErrorHandler * @param bool $logErrorDetails -> Display error details in error log * which can be replaced by a callable of your choice. * Note: This middleware should be added last. It will not handle any exceptions/errors * for middleware added after it. */ $app->addErrorMiddleware(true, true, true); // ... $app->run(); ~~~ ## 新的调度程序和路由结果 We created a wrapper around the FastRoute dispatcher which adds a result wrapper and access to a route’s full list of allowed methods instead of only having access to those when an exception arises. The Request attribute`routeInfo`is now deprecated and replaced with`routingResults`. See[Pull Request #2405](https://github.com/slimphp/Slim/pull/2405)for more information. > 我们围绕FastRoute dispatcher创建了一个包装器,它添加了一个结果包装器,并访问一个路由的允许方法的完整列表,而不是只在异常发生时访问这些方法。请求属性`routeInfo`现在被弃用,代之以`routingResults`。有关更多信息,请参见[Pull Request #2405](https://github.com/slimphp/Slim/pull/2405)。 ~~~php <?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Factory\AppFactory; use Slim\Routing\RouteContext; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $app->get('/hello/{name}', function (Request $request, Response $response) { $routeContext = RouteContext::fromRequest($request); $routingResults = $routeContext->getRoutingResults(); // Get all of the route's parsed arguments e.g. ['name' => 'John'] $routeArguments = $routingResults->getRouteArguments(); // A route's allowed methods are available at all times now and not only when an error arises like in Slim 3 $allowedMethods = $routingResults->getAllowedMethods(); return $response; }); // ... $app->run(); ~~~ ## 新方法覆盖中间件 If you were overriding the HTTP method using either the custom header or the body param, you need to add the`Middleware\MethodOverrideMiddleware`middleware to be able to override the method like before. See[Pull Request #2329](https://github.com/slimphp/Slim/pull/2329)for more information. > 如果您正在使用自定义头或主体参数覆盖HTTP方法,则需要添加“Middleware\MethodOverrideMiddleware”中间件,以便能够像以前那样覆盖该方法。有关更多信息,请参见[Pull Request #2329](https://github.com/slimphp/Slim/pull/2329)。 ~~~php <?php use Slim\Factory\AppFactory; use Slim\Middleware\MethodOverridingMiddleware; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $methodOverridingMiddleware = new MethodOverridingMiddleware(); $app->add($methodOverridingMiddleware); // ... $app->run(); ~~~ ## 新内容长度中间件 The Content Length Middleware will automatically append a`Content-Length`header to the response. This is to replace the`addContentLengthHeader`setting that was removed from Slim 3. This middleware should be placed on the center of the middleware stack so it gets executed last. > 内容长度中间件将自动向响应附加一个“Content-Length”标头。这是为了取代“addContentLengthHeader”设置从Slim 3删除。这个中间件应该放在中间件堆栈的中心,这样它才会最后执行。 ~~~php <?php use Slim\Factory\AppFactory; use Slim\Middleware\ContentLengthMiddleware; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $contentLengthMiddleware = new ContentLengthMiddleware(); $app->add($contentLengthMiddleware); // ... $app->run(); ~~~ ## 新的输出缓冲中间件 The Output Buffering Middleware enables you to switch between two modes of output buffering:`APPEND`(default) and`PREPEND`mode. The`APPEND`mode will use the existing response body to append the content while`PREPEND`mode will create a new response body and append it to the existing response. This middleware should be placed on the center of the middleware stack so it gets executed last. > 输出缓冲中间件允许您在两种输出缓冲模式之间进行切换:`APPEND `(默认)和`PREPEND`模式。`APPEND`模式将使用现有的响应体来附加内容,而`PREPEND`模式将创建一个新的响应体并将其附加到现有响应中。这个中间件应该放在中间件堆栈的中心,这样它才会最后执行。 ~~~php <?php use Slim\Factory\AppFactory; use Slim\Middleware\OutputBufferingMiddleware; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); /** * The two modes available are * OutputBufferingMiddleware::APPEND (default mode) - Appends to existing response body * OutputBufferingMiddleware::PREPEND - Creates entirely new response body */ $mode = OutputBufferingMiddleware::APPEND; $outputBufferingMiddleware = new OutputBufferingMiddleware($mode); // ... $app->run(); ~~~