cobaia / cobaia-annotation
CakePHP 插件,包含一系列可使用的注解
dev-master
2013-02-04 02:37 UTC
Requires
- php: >=5.3.0
- composer/installers: *
- doctrine/annotations: 1.0
This package is not auto-updated.
Last update: 2024-09-14 13:35:28 UTC
README
CakePHP 中的注解。
为什么?
- 因为注解很酷!
- 因为我们喜欢(至少我喜欢)
- 因为在 PHP 中反复重复相同的代码没有意义
- 使用 PHP 数组进行配置很糟糕
- 元数据和元编程很酷!
安装
CobaiaAnnotations 使用 composer,因此下载 composer 并创建您的 app/composer.json
{ "name": "testing", "require": { "php": ">=5.3.0", "cakephp/debug_kit" : "*", "cobaia/cobaia-annotation": "dev-master" }, "minimum-stability": "dev" }
现在是时候运行 composer.phar install
安装后,现在是加载插件的时候了,打开 app/Config/bootstrap.php
CakePlugin::load('CobaiaAnnotation', array('bootstrap' => true)); //register filter Configure::write('Dispatcher.filters', array( 'AssetDispatcher', 'CacheDispatcher', 'CobaiaAnnotation.AnnotationDispatcher' ));
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 会自动将操作参数转换为数据库中的数据。
例如,使用参数转换器:调用:/contents/view/20
将等同于
$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 会处理你的视图,忘记 $this->layout
,$this->render()
,只需在注解中定义你想要使用的布局和视图,这样就完成了。
它还支持继承,如果在类的 DocBlock 中定义了一个布局或视图,则操作将继承该值。
@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 将处理模型、组件和帮助器的加载,不再需要在代码中放置类的属性。
接下来是什么?
- @Route (在控制器中添加自定义路由)
- @Auth (允许,拒绝)
- @Route (在控制器中添加自定义路由)
- @Auth (允许,拒绝)
- @Table
- @HasMany
- @HasAndBelongsToMany
- @BelongsTo
- @Behaviors
- 使用 2 个自动加载检查性能
- 缓存解析注解
- 添加单元测试
许可证
许可协议为 The MIT License 文件重新分发必须保留上述版权声明。
作者
Vinícius Krolow - krolow[at]gmail.com