CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • AssetMiddleware
  • RoutingMiddleware
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.3.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\Routing\Middleware;
 16: 
 17: use Cake\Cache\Cache;
 18: use Cake\Core\HttpApplicationInterface;
 19: use Cake\Core\PluginApplicationInterface;
 20: use Cake\Http\MiddlewareQueue;
 21: use Cake\Http\Runner;
 22: use Cake\Routing\Exception\RedirectException;
 23: use Cake\Routing\Router;
 24: use Psr\Http\Message\ResponseInterface;
 25: use Psr\Http\Message\ServerRequestInterface;
 26: use Zend\Diactoros\Response\RedirectResponse;
 27: 
 28: /**
 29:  * Applies routing rules to the request and creates the controller
 30:  * instance if possible.
 31:  */
 32: class RoutingMiddleware
 33: {
 34:     /**
 35:      * Key used to store the route collection in the cache engine
 36:      */
 37:     const ROUTE_COLLECTION_CACHE_KEY = 'routeCollection';
 38: 
 39:     /**
 40:      * The application that will have its routing hook invoked.
 41:      *
 42:      * @var \Cake\Core\HttpApplicationInterface|null
 43:      */
 44:     protected $app;
 45: 
 46:     /**
 47:      * The cache configuration name to use for route collection caching,
 48:      * null to disable caching
 49:      *
 50:      * @var string|null
 51:      */
 52:     protected $cacheConfig;
 53: 
 54:     /**
 55:      * Constructor
 56:      *
 57:      * @param \Cake\Core\HttpApplicationInterface|null $app The application instance that routes are defined on.
 58:      * @param string|null $cacheConfig The cache config name to use or null to disable routes cache
 59:      */
 60:     public function __construct(HttpApplicationInterface $app = null, $cacheConfig = null)
 61:     {
 62:         if ($app === null) {
 63:             deprecationWarning(
 64:                 'RoutingMiddleware should be passed an application instance. ' .
 65:                 'Failing to do so can cause plugin routes to not behave correctly.'
 66:             );
 67:         }
 68:         $this->app = $app;
 69:         $this->cacheConfig = $cacheConfig;
 70:     }
 71: 
 72:     /**
 73:      * Trigger the application's routes() hook if the application exists and Router isn't initialized.
 74:      * Uses the routes cache if enabled via configuration param "Router.cache"
 75:      *
 76:      * If the middleware is created without an Application, routes will be
 77:      * loaded via the automatic route loading that pre-dates the routes() hook.
 78:      *
 79:      * @return void
 80:      */
 81:     protected function loadRoutes()
 82:     {
 83:         if (!$this->app) {
 84:             return;
 85:         }
 86: 
 87:         $routeCollection = $this->buildRouteCollection();
 88:         Router::setRouteCollection($routeCollection);
 89:     }
 90: 
 91:     /**
 92:      * Check if route cache is enabled and use the configured Cache to 'remember' the route collection
 93:      *
 94:      * @return \Cake\Routing\RouteCollection
 95:      */
 96:     protected function buildRouteCollection()
 97:     {
 98:         if (Cache::enabled() && $this->cacheConfig !== null) {
 99:             return Cache::remember(static::ROUTE_COLLECTION_CACHE_KEY, function () {
100:                 return $this->prepareRouteCollection();
101:             }, $this->cacheConfig);
102:         }
103: 
104:         return $this->prepareRouteCollection();
105:     }
106: 
107:     /**
108:      * Generate the route collection using the builder
109:      *
110:      * @return \Cake\Routing\RouteCollection
111:      */
112:     protected function prepareRouteCollection()
113:     {
114:         $builder = Router::createRouteBuilder('/');
115:         $this->app->routes($builder);
116:         if ($this->app instanceof PluginApplicationInterface) {
117:             $this->app->pluginRoutes($builder);
118:         }
119: 
120:         return Router::getRouteCollection();
121:     }
122: 
123:     /**
124:      * Apply routing and update the request.
125:      *
126:      * Any route/path specific middleware will be wrapped around $next and then the new middleware stack will be
127:      * invoked.
128:      *
129:      * @param \Psr\Http\Message\ServerRequestInterface $request The request.
130:      * @param \Psr\Http\Message\ResponseInterface $response The response.
131:      * @param callable $next The next middleware to call.
132:      * @return \Psr\Http\Message\ResponseInterface A response.
133:      */
134:     public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
135:     {
136:         $this->loadRoutes();
137:         try {
138:             Router::setRequestContext($request);
139:             $params = (array)$request->getAttribute('params', []);
140:             $middleware = [];
141:             if (empty($params['controller'])) {
142:                 $parsedBody = $request->getParsedBody();
143:                 if (is_array($parsedBody) && isset($parsedBody['_method'])) {
144:                     $request = $request->withMethod($parsedBody['_method']);
145:                 }
146:                 $params = Router::parseRequest($request) + $params;
147:                 if (isset($params['_middleware'])) {
148:                     $middleware = $params['_middleware'];
149:                     unset($params['_middleware']);
150:                 }
151:                 $request = $request->withAttribute('params', $params);
152:             }
153:         } catch (RedirectException $e) {
154:             return new RedirectResponse(
155:                 $e->getMessage(),
156:                 (int)$e->getCode(),
157:                 $response->getHeaders()
158:             );
159:         }
160:         $matching = Router::getRouteCollection()->getMiddleware($middleware);
161:         if (!$matching) {
162:             return $next($request, $response);
163:         }
164:         $matching[] = $next;
165:         $middleware = new MiddlewareQueue($matching);
166:         $runner = new Runner();
167: 
168:         return $runner->run($middleware, $request, $response);
169:     }
170: }
171: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs