<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 升級指南 如果你打算從 Slim 2 升級到 Slim 3,這里有一些重要的變化,你必須清楚。 ## 新的 PHP 版本要求 Slim 3 要求 PHP 5.5+ ## 新的路由函數簽名 ``` $app->get('/', function (Request $req, Response $res, $args = []) { return $res->withStatus(400)->write('Bad Request'); }); ``` ## 獲取 _GET 和 _POST 變量 ``` $app->get('/', function (Request $req, Response $res, $args = []) { $myvar1 = $req->getParam('myvar'); //檢查 _GET 和 _POST [不遵循 PSR 7] $myvar2 = $req->getParsedBody()['myvar']; //檢查 _POST [遵循 PSR 7] $myvar3 = $req->getQueryParams()['myvar']; //檢查 _GET [遵循 PSR 7] }); ``` ## 鉤子 / Hooks Slim v3 不再有鉤子的概念。You should consider reimplementing any functionality associated with the [default hooks in Slim v2](http://docs.slimframework.com/hooks/defaults/) as [middleware](/docs/concepts/middleware.html) instead. If you need the ability to apply custom hooks at arbitrary points in your code (for example, within a route), you should consider a third-party package such as [Symfony’s EventDispatcher](http://symfony.com/doc/current/components/event_dispatcher/introduction.html) or [Zend Framework’s EventManager](https://zend-eventmanager.readthedocs.org/en/latest/). ## 移除 HTTP 緩存 在 Slim v3 我們將 HTTP 緩存遷移到了單獨的模塊中: [Slim\Http\Cache](https://github.com/slimphp/Slim-HttpCache). ## 移除 Stop/Halt Slim Core has removed Stop/Halt. In your applications, you should transition to using the withStatus() and withBody() methods. ## 重定向的改變 在 Slim v2.x 我們需要使用助手函數 $app-&gt;redirect(); 來觸發重定向請求。在 Slim v3.x 中,我們可以使用響應類來做這事。 Example: ``` $app->get('/', function ($req, $res, $args) { return $res->withStatus(302)->withHeader('Location', 'your-new-uri'); }); ``` ## 中間件簽名 中間件的簽名已經從一個類變成了函數。 新的簽名: ``` use Psr\Http\Message\RequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; $app->add(function (Request $req, Response $res, callable $next) { // Do stuff before passing along $newResponse = $next($req, $res); // Do stuff after route is rendered return $newResponse; // continue }); ``` 你仍然可以使用類來做簽名: ``` namespace My; use Psr\Http\Message\RequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; class Middleware { function __invoke(Request $req, Response $res, callable $next) { // Do stuff before passing along $newResponse = $next($req, $res); // Do stuff after route is rendered return $newResponse; // continue } } // Register $app->add(new My\Middleware()); // or $app->add(My\Middleware::class); ``` ## Middleware Execution Application middleware is executed as Last In First Executed (LIFE). ## Flash Messages Flash messages are no longer a part of the Slim v3 core but instead have been moved to seperate [Slim Flash](/docs/features/flash.html) package. ## Cookies In v3.0 cookies has been removed from core. See [FIG Cookies](https://github.com/dflydev/dflydev-fig-cookies) for a PSR-7 compatible cookie component. ## Removal of Crypto In v3.0 we have removed the dependency for crypto in core. ## New Router Slim now utilizes [FastRoute](https://github.com/nikic/FastRoute), a new, more powerful router! This means that the specification of route patterns has changed with named parameters now in braces and square brackets used for optional segments: ``` // named parameter: $app->get('/hello/{name}', /*...*/); // optional segment: $app->get('/news[/{year}]', /*...*/); ``` ## Route Middleware The syntax for adding route middleware has changed slightly. In v3.0: ``` $app->get(…)->add($mw2)->add($mw1); ``` ## urlFor() is now pathFor() in the router `urlFor()` has been renamed `pathFor()` and can be found in the `router` object: ``` $app->get('/', function ($request, $response, $args) { $url = $this->router->pathFor('home'); $response->write("<a href='$url'>Home</a>"); return $response; })->setName('home'); ``` Also, `pathFor()` is base path aware. ## Container and DI … Constructing Slim uses Pimple as a Dependency Injection Container. ``` // index.php $app = new Slim\App( new \Slim\Container( include '../config/container.config.php' ) ); // Slim will grab the Home class from the container defined below and execute its index method. // If the class is not defined in the container Slim will still contruct it and pass the container as the first arugment to the constructor! $app->get('/', Home::class . ':index'); // In container.config.php // We are using the SlimTwig here return [ 'settings' => [ 'viewTemplatesDirectory' => '../templates', ], 'twig' => [ 'title' => '', 'description' => '', 'author' => '' ], 'view' => function ($c) { $view = new Twig( $c['settings']['viewTemplatesDirectory'], [ 'cache' => false // '../cache' ] ); // Instantiate and add Slim specific extension $view->addExtension( new TwigExtension( $c['router'], $c['request']->getUri() ) ); foreach ($c['twig'] as $name => $value) { $view->getEnvironment()->addGlobal($name, $value); } return $view; }, Home::class => function ($c) { return new Home($c['view']); } ]; ``` ## PSR-7 Objects ### Request, Response, Uri & UploadFile are immutable. This means that when you change one of these objects, the old instance is not updated. ``` // This is WRONG. The change will not pass through. $app->add(function (Request $request, Response $response, $next) { $request->withAttribute('abc', 'def'); return $next($request, $response); }); // This is correct. $app->add(function (Request $request, Response $response, $next) { $request = $request->withAttribute('abc', 'def'); return $next($request, $response); }); ``` ### Message bodies are streams ``` // ... $image = __DIR__ . '/huge_photo.jpg'; $body = new Stream($image); $response = (new Response()) ->withStatus(200, 'OK') ->withHeader('Content-Type', 'image/jpeg') ->withHeader('Content-Length', filesize($image)) ->withBody($body); // ... ``` For text: ``` // ... $response = (new Response())->getBody()->write('Hello world!') // Or Slim specific: Not PSR-7 compliant. $response = (new Response())->write('Hello world!'); // ... ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看