Awesome
Cake Factory
Cake is a CakePHP plugin which provides an additional factory for the Cake Toolkit (CTK), adding configurable objects for the core Html and Form helpers available with the framework.
The inclusion of an object-oriented interface for the core helpers allows for a more sophisticated usage, such as adding child nodes and binding events, as well as improving the overall level of abstraction and separation of concerns in the View layer.
Requirements
- CakePHP 2+
- PHP 5.3+
- Cake Toolkit (https://github.com/jameswatts/cake-toolkit)
Installation
To use the plugin simply include it in your application's "app/Plugin" directory, and load it in the "app/Config/bootstrap.php" file.
CakePlugin::load('CakeFactory');
The above code is not required if you're already using CakePlugin::loadAll()
to load all plugins.
Implementation
Once the plugin is available it's ready to use in your CTK Views, or with the Factory helper included with the Cake Toolkit. To include the Cake factory in a View just add the factory to your $factories collection, for example:
public $factories = array('CakeFactory.Cake');
With the factory now available you can call it in your View, and build your application using the Html and Form helpers via an object-oriented interface.
Here's a simple example of creating a link from the Html helper:
$this->Cake->Link(array(
'title' => __('Read more'),
'url' => array(
'controller' => 'Posts',
'action' => 'view',
$postId
)
));
Another example, creating a form with an input from the Form helper, with the additional binding of an event to the input element:
// create a CakePHP form
$form = $this->Cake->Form(array(
'model' => 'Example',
'options' => array(
'action' => 'add'
)
));
// create a CakePHP input
$input = $this->Cake->Input(array(
'field' => 'Example.column',
'type' => 'text'
));
// bind an event to the input
$input->bind('keyup', $this->Js->Alert(array(
'code' => $this->Js->Element(array('node' => $input))->getValue()
)));
// add the input to the form
$form->add($input);
// add the form to the view
$this->add($form);
Support
For support, bugs and feature requests, please use the issues section of this repository.
Licence
Copyright 2013 James Watts (CakeDC). All rights reserved.
Licensed under the MIT License. Redistributions of the source code included in this repository must retain the copyright notice found in each file.
Acknowledgements
Thanks to Larry Masters and everyone who has contributed to CakePHP, helping make this framework what it is today.