dr-benton / before-after-controllers-hooks-bundle
为 Symfony 控制器提供 "@BeforeHook" 和 "@AfterHook" 注解支持
Requires
- doctrine/annotations: ~1.2
- doctrine/common: ~2.4
- symfony/framework-bundle: ~2.5
Requires (Dev)
- behat/behat: ~3.0
This package is not auto-updated.
Last update: 2020-03-02 04:35:55 UTC
README
如果你喜欢 Silex 路由中间件 或 Ruby on Rails 动作过滤器,你可能喜欢这个 Bundle,因为它在 Symfony2 中模仿了这种行为,多亏了特定的注解。
这种围绕控制器操作的“之前”和“之后”回调可能不符合“设计模式”,但它很实用,并允许轻松应用 DRY 原则 :-)
使用 "@BeforeHook" 和 "@AfterHook" 注解,您可以触发控制器本身的方法或 Symfony 服务的方法。
如果 "@BeforeHook" 回调返回一个响应对象,则请求处理被短路(不会运行下一个钩子,也不会执行控制器操作),响应立即传递给 "@AfterHook" 回调 - 或者如果没有与该控制器操作链接的 @AfterHook,则返回给客户端。
摘要
如往常一样,一个代码示例可能胜过千言万语
<?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>' ); } } }
安装
步骤 1: Composer
将以下行添加到 composer.json
文件中
{ "require": { "dr-benton/before-after-controllers-hooks-bundle": "@dev-master" } }
然后运行
$ composer update "dr-benton/before-after-controllers-hooks-bundle"
步骤 2: 启用 Bundle
最后,在内核中启用 Bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new DrBenton\Bundle\BeforeAfterControllersHooksBundle\BeforeAfterControllersHooksBundle(), ); }
许可
版权所有 (c) 2014 Olivier Philippon
在此,任何人获得此软件及其相关文档文件(“软件”)的副本(以下简称“软件”),均可免费使用该软件,不受限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,以及允许获得软件的人进行此类操作的权利,但受以下条件约束
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的还是与特定目的相关的,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论该责任是由于合同、侵权或其他原因引起的,无论该责任与软件或其使用或其他交易有关。