dr-benton/before-after-controllers-hooks-bundle
最新稳定版本:v1.0.0
Composer 安装命令:
composer require dr-benton/before-after-controllers-hooks-bundle
包简介
Provides "@BeforeHook" and "@AfterHook" Annotations support for Symfony Controllers
README 文档
README
If you like Silex routes middlewares or Ruby on Rails actions filters, you may appreciate this Bundle, as it mimics this behaviour in Symfony2, thanks to specific Annotations.
This type of "before" and "after" callbacks around Controllers Actions may not be very "design patterns compliant", but it's pragmatic and allows easy DRY principle application :-)
With the "@BeforeHook" and "@AfterHook" Annotations, you can trigger methods of the Controller itself, or Symfony Services methods.
If a "@BeforeHook" callback returns a Response object, the Request handling is short-circuited (the next hooks won't be run, neither the Controller action), and the Response is passed to the "@AfterHook" callback(s) right away - or returned to the client if no @AfterHook is linked to this Controller Action.
Synopsis
As always, a code sample may be worth a thousand words:
<?php namespace AppBundle\Controller; use DrBenton\Bundle\BeforeAfterControllersHooksBundle\Annotation\BeforeControllerHook as BeforeHook; use DrBenton\Bundle\BeforeAfterControllersHooksBundle\Annotation\AfterControllerHook as AfterHook; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; /** * Before any of this Controller Action, its 'checkSomething' method * will be triggered: * * @BeforeHook("checkSomething") */ class BooksController extends Controller { /** * The Controller 'checkBooksAvailability()' method will be triggered * before this Controller action: * * @BeforeHook("checkBooksAvailability") * @Template() */ public function indexAction() { $books = $this->getDoctrine() ->getRepository('AppBundle:Book') ->findAll(); return ['books' => $books]; } /** * You can also send params to the triggered hook: * * @BeforeHook("doSomethingBeforeAction", args={"param1", "param2"}) * @Template() */ public function showAction(Book $book) { return ['book' => $book]; } /** * Want to trigger a Symfony Service method? No problem! * Just use the "@[serviceId]::[methodName]" notation: * * @BeforeHook("@logger::addInfo", args={"showComments() will be called"}) * @Template() */ public function showCommentsAction(Book $book) { return ['book' => $book]; } /** * You can also trigger a custom callable after the Controller action: * * @AfterHook("addDebugCodeAfterAction") * @Template() */ public function showSomethingAction(Book $book) { return ['book' => $book]; } /** * You can use Services here too, and use params. Any "%response%" param * will be replaced with the Controller's returned Symfony Reponse. * * @AfterHook("@my_service::doSomethingAfterAction", args={"%response%", {"key" => "value"}}) * @Template() */ public function showSomethingAction(Book $book) { return ['book' => $book]; } protected function checkSomething() { // Do something here... // It this method returns a Symfony Response, the Controller // will be short-circuited and this Response will be sent to the client. } protected function checkBooksAvailability() { // idem: return a Response here if oy want to short-circuit the Controller } protected function doSomethingBeforeAction($arg1, $arg2) { // Do something here... } protected function addDebugCodeAfterAction(Response $controllerResponse) { if ($this->container->getParameter('debug')) { $controllerResponse->setContent( $controllerResponse->getContent() . '<script src="//assets/js/debug.js"></script>' ); } } }
Installation
Step 1: Composer
Add the following line to the composer.json file:
{
"require": {
"dr-benton/before-after-controllers-hooks-bundle": "@dev-master"
}
}
Then run:
$ composer update "dr-benton/before-after-controllers-hooks-bundle"
Step 2: Enable the bundle
Finally, enable the bundle in the kernel:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new DrBenton\Bundle\BeforeAfterControllersHooksBundle\BeforeAfterControllersHooksBundle(), ); }
License
Copyright (c) 2014 Olivier Philippon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
dr-benton/before-after-controllers-hooks-bundle 适用场景与选型建议
dr-benton/before-after-controllers-hooks-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.48k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 10 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dr-benton/before-after-controllers-hooks-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dr-benton/before-after-controllers-hooks-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 27.48k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-10-26