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

  • ActionDispatcher
  • BaseApplication
  • Client
  • ControllerFactory
  • CorsBuilder
  • MiddlewareQueue
  • Response
  • ResponseEmitter
  • Runner
  • Server
  • ServerRequest
  • ServerRequestFactory
  • Session
  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\Http;
 16: 
 17: use Cake\Core\App;
 18: use Cake\Routing\Exception\MissingControllerException;
 19: use Cake\Utility\Inflector;
 20: use ReflectionClass;
 21: 
 22: /**
 23:  * Factory method for building controllers from request/response pairs.
 24:  */
 25: class ControllerFactory
 26: {
 27:     /**
 28:      * Create a controller for a given request/response
 29:      *
 30:      * @param \Cake\Http\ServerRequest $request The request to build a controller for.
 31:      * @param \Cake\Http\Response $response The response to use.
 32:      * @return \Cake\Controller\Controller
 33:      * @throws \ReflectionException
 34:      */
 35:     public function create(ServerRequest $request, Response $response)
 36:     {
 37:         $className = $this->getControllerClass($request);
 38:         if (!$className) {
 39:             $this->missingController($request);
 40:         }
 41:         $reflection = new ReflectionClass($className);
 42:         if ($reflection->isAbstract() || $reflection->isInterface()) {
 43:             $this->missingController($request);
 44:         }
 45: 
 46:         /** @var \Cake\Controller\Controller $controller */
 47:         $controller = $reflection->newInstance($request, $response);
 48: 
 49:         return $controller;
 50:     }
 51: 
 52:     /**
 53:      * Determine the controller class name based on current request and controller param
 54:      *
 55:      * @param \Cake\Http\ServerRequest $request The request to build a controller for.
 56:      * @return string|null
 57:      */
 58:     public function getControllerClass(ServerRequest $request)
 59:     {
 60:         $pluginPath = $controller = null;
 61:         $namespace = 'Controller';
 62:         if ($request->getParam('controller')) {
 63:             $controller = $request->getParam('controller');
 64:         }
 65:         if ($request->getParam('plugin')) {
 66:             $pluginPath = $request->getParam('plugin') . '.';
 67:         }
 68:         if ($request->getParam('prefix')) {
 69:             if (strpos($request->getParam('prefix'), '/') === false) {
 70:                 $namespace .= '/' . Inflector::camelize($request->getParam('prefix'));
 71:             } else {
 72:                 $prefixes = array_map(
 73:                     'Cake\Utility\Inflector::camelize',
 74:                     explode('/', $request->getParam('prefix'))
 75:                 );
 76:                 $namespace .= '/' . implode('/', $prefixes);
 77:             }
 78:         }
 79:         $firstChar = substr($controller, 0, 1);
 80: 
 81:         // Disallow plugin short forms, / and \\ from
 82:         // controller names as they allow direct references to
 83:         // be created.
 84:         if (strpos($controller, '\\') !== false ||
 85:             strpos($controller, '/') !== false ||
 86:             strpos($controller, '.') !== false ||
 87:             $firstChar === strtolower($firstChar)
 88:         ) {
 89:             $this->missingController($request);
 90:         }
 91: 
 92:         return App::className($pluginPath . $controller, $namespace, 'Controller') ?: null;
 93:     }
 94: 
 95:     /**
 96:      * Throws an exception when a controller is missing.
 97:      *
 98:      * @param \Cake\Http\ServerRequest $request The request.
 99:      * @throws \Cake\Routing\Exception\MissingControllerException
100:      * @return void
101:      */
102:     protected function missingController($request)
103:     {
104:         throw new MissingControllerException([
105:             'class' => $request->getParam('controller'),
106:             'plugin' => $request->getParam('plugin'),
107:             'prefix' => $request->getParam('prefix'),
108:             '_ext' => $request->getParam('_ext')
109:         ]);
110:     }
111: }
112: 
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