CakePHP 3 the Way to Reach Controller Invoking part 1 of 3


I researched executed process in CakePHP 3, by controller execution. It was really complicated than I expected, so I omitted some explanation, and I guess it is somehow difficult to read this article, sorry.

Environment

  • PHP 5.5.9
  • cakephp/cakephp 3.0.9
  • Ubuntu 14.04.2 LTS

I founded server as “php bin/cake.php server“. When the server receive a request and CakePHP 3 create html output, webroot/index.php is executed first. Let’s look into index.php.

index.php

bootstrap.php setup basic environment and dispatcher created by DispatcherFactory handles request.

I picked up the important part of bootstrap.php.

bootstrap.php

Plugin::load and DispatcherFactory::add registers distcher filters. Plugin::load('DebugKit', ['bootstrap' => true]); works according to settings in app.php. app.php is a sort of configuration file. (related article: CakePHP3 Config File for Each Environment)。

Plugin::load

Let’s read comment.

From the comment, we can know Plugin::load('DebugKit', ['bootstrap' => true]) execute bootstrap.php in every plugins.

DispatcherFactory::add($debugBar) registers DebugBarFilter as a filter of DispatcherFactory. Until to reach DispatcherFactory::add($debugBar), load several configuration values, which change according to app.php.

After loading DebugKit, it adds 3 kinds of class with DispatcherFactory::add.

DispatcherFactory::add

This method add middle-ware object to be executed.

All of arguments for add method are string, 'Asset', 'Routing', 'ControllerFactory'. So the return value of _createFilter method will be added into static::$_stack.

Let’s see the return value of _createFilter.

_createFilter

This method creates an instance of filter class, it is obvious from the comment.

App::className generates class name with namespace from argument. I wrote more detail in the article CakePHP 3 read. In this case, add method is called with argument 'Asset', 'Routing', 'ControllerFactory', and added Routing\Filter\AssetFilter, Routing\Filter\RoutingFilter, Routing\Filter\ControllerFactoryFilter were returned as return value of _createFilter method.

As for now, 4 instance were inserted into static::$_stack.

  • DebugKit\Routing\Filter\DebugBarFilter
  • Cake\Routing\Filter\AssetFilter
  • Cake\Routing\Filter\RoutingFilter
  • Cake\Routing\Filter\ControllerFactoryFilter

The remain code of index.php is the following 5 lines.

Continued to CakePHP 3 the Way to Reach Controller Invoking Part 2 of 3.