cobaia / cobaia-annotation
CakePHP Plugin with a collection of Annotations to use
Installs: 11
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 4
Type:cakephp-plugin
Requires
- php: >=5.3.0
- composer/installers: *
- doctrine/annotations: 1.0
This package is not auto-updated.
Last update: 2024-11-09 14:32:11 UTC
README
Annotations in CakePHP.
Why?
- Because Annotations are cool!
- Because We like (or at least I like)
- Because no make sense repeate over and over the same code in PHP
- Use PHP array to configure is sux
- Metadata and Metaprogramming are cool!
Installing
CobaiaAnnotations make usage of composer, so download composer and create your app/composer.json
{ "name": "testing", "require": { "php": ">=5.3.0", "cakephp/debug_kit" : "*", "cobaia/cobaia-annotation": "dev-master" }, "minimum-stability": "dev" }
It's time to running composer.phar install
After install it's time to load the plugin, open app/Config/bootstrap.php
CakePlugin::load('CobaiaAnnotation', array('bootstrap' => true)); //register filter Configure::write('Dispatcher.filters', array( 'AssetDispatcher', 'CacheDispatcher', 'CobaiaAnnotation.AnnotationDispatcher' ));
What annotations do we have in CobaiaAnnotation?
@ParamConverter
<?php App::uses('AppController', 'Controller'); use CobaiaAnnotation\Configuration\Controller\ParamConverter; class ContentsController extends AppController { /** * @ParamConverter("content", class="Content") */ public function view($content = 1) { var_dump($content); } }
ParamConverter will automatically converts the action paramter to data that is in the database.
So for example, using param coverter: call: /contents/view/20
It would be the same as
$content = $this->Content->read(null, $content);
@ViewHandler
<?php App::uses('AppController', 'Controller'); use CobaiaAnnotation\Configuration\Controller\ViewHandler; /** * @ViewHandler(layout="ajax") */ class ContentsController extends AppController { /** * @ViewHandler(view="show") */ public function view() { } /** * @ViewHandler(layout="default") */ public function index() { } }
ViewHandler will take care of your view, forget about $this->layout
, $this->render()
, just define in the annotation what layout you want to use and also what view, and that's it.
It makes also inheritance so if you define one layout or view in the class DocBlock, the actions will inherit the value.
@ModelLoader, @ComponentLoader, @HelperLoader
<?php App::uses('AppController', 'Controller'); use CobaiaAnnotation\Configuration\Controller\Loader\ModelLoader; use CobaiaAnnotation\Configuration\Controller\Loader\ComponentLoader; use CobaiaAnnotation\Configuration\Controller\Loader\HelperLoader; /** * @ModelLoader({"Content", "Fake"}) * @ComponentLoader({"Session", "RequestHandler"}) * @HelperLoader({"Text", "Time", "Number"}) */ class ContentsController extends AppController { public function view($content = 1) { } }
@Model, @Component, @Helper will handle to you the load of models, components and helpers, not needed anymore put the attribute of class in your code.
What next?
- @Route (adding custom routes in your controller)
- @Auth (allow, deny)
- @Route (adding custom routes in your controller)
- @Auth (allow, deny)
- @Table
- @HasMany
- @HasAndBelongsToMany
- @BelongsTo
- @Behaviors
- Check performance with 2 autoloads
- Cache annotations parsed
- Add Unit Tests
License
Licensed under The MIT License Redistributions of files must retain the above copyright notice.
Author
VinÃcius Krolow - krolow[at]gmail.com