The Cake Toolkit is a CakePHP plugin designed for medium-to-large scale web applications which require a scalable and standard solution to handling Views., (*1)
The plugin allows Views to be defined as a class, providing a powerful object-oriented factory interface to dynamically build your application's UI with configurable objects., (*2)
The main features of the plugin include:, (*3)
-
Object-Oriented Design: Every UI element is defined and manipulated as an object, giving you maximum control over the development of your View, by limiting your work to the semantic definition of the interface.
-
Abstraction and Encapsulation: The ability to define a View as a native PHP class unlocks the potential of the native object-oriented features of the language, allowing Views to be extended, blocks of content to be abstracted, and view logic to be encapsulated in helper methods.
-
Separation of Concerns: The design of the plugin provides a clear and coherent separation of the languages implicated in your development, such as PHP, HTML, CSS and JavaScript, making it easy to delegate experts in their field to the relevant areas, without having to work around code they're not familiar with.
-
Extensible Architecture: Allowing collections of UI elements to be packaged into factories makes it easy to, not only extend the existing factories but, also create your own, tailored to your requirements.
-
Plug and Play: The functionality of the plugin only adds to the current features available in CakePHP, making it possible for CTK Views to work along-side normal static ".ctp" Views.
-
Legacy Features: Great care has been taken to maintaining the availability of the standard features for Views available in CakePHP, such as layouts, elements, content blocks and helpers.
To start using the Cake Toolkit it's as simple as including the plugin in your application, and then extending the CtkAppController to inherit everything you'll need, for example:, (*4)
class ExampleController extends CtkAppController {
public function index() {
// action logic
}
}
Or, alternatively, using the AppComponent for existing controllers:, (*5)
public $components = array('Ctk.App');
You can then define your View as a class, and build your UI using the objects made available through the included factories, without requiring extensive knowledge of the underlying technologies, for example:, (*6)
class IndexView extends CtkView {
public $factories = array('Html', 'Js');
public function build() {
// create a HTML div
$div = $this->Html->Div();
// create a HTML button
$button = $this->Html->Button(array(
'value' => __('Click Me')
));
// add an event to the button
$button->addEvent('click', $this->Js->Alert(array(
'text' => __('Hello World')
)));
// add the button to the div
$div->add($button);
// add the div to the view
$this->add($div);
}
}
It's also possible to use the special Factory helper to import objects from the Cake Toolkit into normal static ".ctp" Views, bringing the power of the plugin to your existing View code., (*7)
public $helpers = array('Ctk.Factory');
You can then use the objects within your markup, similar to when using the HTML helper, for example:, (*8)
<div id="example">
<?php echo $this->Factory->Html->Span(array('text' => __('Hello World'))); ?>
</div>
To get up and running quickly check out the Quick Start or Hello World tutorials from the wiki., (*9)
Requirements
Documentation
Once installed, the Cake Toolkit has a homepage built into the CakePHP plugin itself. To access the homepage, visit /ctk
from your application base, for example:, (*10)
http://example.com/ctk
Full documentation, as well as tutorials, are also available from the wiki section of this repository., (*11)
As with all modern web applications, performance is an important issue to take into consideration, specially if you're running a large enterprise scale application which has high development and maintenance costs associated with it., (*12)
It's important to note that the aim of the Cake Toolkit is not to increase the performance of the View layer in the MVC stack, but to improve the effectiveness of the development, as well as decrease the maintenance required for large scale applications. CakePHP already has very powerful and configurable caching mechanisms available, which are also available to CTK when building your Views, and strongly recommended due to the heavy use of objects by the plugin., (*13)
To quickly add caching to your View, simply define the $cacheAction property in your Controller, and define the settings for the actions you wish to cache. For example, with an action called "example", you can define a cache time of 1 week with the following:, (*14)
public $cacheAction = array(
'example' => array('duration' => 36000)
);
Packed with the plugin is a Benchmark controller which provides various actions which can be scaled at runtime to view the overall performance impact:, (*15)
-
single: This action simply creates a series XML nodes. It accepts a single argument, which is the number of nodes to generate.
-
lineal: This action creates a multi-dimensional sequence of XML nodes in a lineal hierarchy. It accepts a single argument, which is the number of levels of nodes to generate.
-
exponential: This action creates a multi-dimentional sequence of XML nodes in an exponential hierarchy. It accepts a single argument, which is the number of exponential factors to generate.
-
matrix: This action displays a matrix of data in a HTML table. It accepts 2 arguments, the first is the number of rows to generate, and the second the number of cells in each row.
When "debug" is set to any value higher than 0, and assuming caching is NOT enabled, the plugin will also add a Ctk-Info header to the HTTP response, for example:, (*16)
Ctk-Info: controller=Benchmark, action=single, render-time=0.019, total-memory=6.86MB, memory-usage=1.83MB
This header contains the following values:, (*17)
-
controller: The name of the Controller called.
-
action: The name of the action executed.
-
render-time: The time in seconds it took to render the View.
-
total-memory: The total amount of memory used until rendering was complete.
-
memory-usage: The memory consumed specifically by the plugin.
The following table shows a basic performance scalability benchmark based on the single action of the Benchmark Controller, without caching enabled:, (*18)
|
1 Node |
10 Nodes |
100 Nodes |
1000 Nodes |
10000 Nodes |
Render Time |
0.019 |
0.024 |
0.030 |
0.236 |
2.386 |
Total Memory |
6.86 MB |
6.89 MB |
7.17 MB |
10.03 MB |
38.98 MB |
Memory Usage |
1.83 MB |
1.86 MB |
2.14 MB |
5.01 MB |
33.96 MB |
These tests were performed on a Intel Pentium laptop using 4 GB of RAM, with the maximum memory setting of PHP set to 128 MB., (*19)
IMPORTANT: Remember that caching is NOT enabled on this controller, as viewing the changes to the action's output is vital to purpose of its use. Turning on the cache for an action of a CTK Controller is almost identical in performance to that of a standard static ".ctp" View, as the plugin uses the same routing for the cached files., (*20)
Support
For support, bugs and feature requests, please use the issues section of this repository., (*21)
Licence
Copyright 2012 James Watts (CakeDC). All rights reserved., (*22)
Licensed under the MIT License. Redistributions of the source code included in this repository must retain the copyright notice found in each file., (*23)
Acknowledgements
A special thanks to Larry Masters, the founder of CakePHP, for his help and support, to José Lorenzo for his input early on when I was hacking the framework, to Florian KrÀmer for his constructive critisism, as well as the entire CakeDC team for their feedback and encouragement., (*24)