aferrandini / disable-bundle
Symfony2 扩展包,用于禁用带注解的控制器的功能。
v1.0
2013-07-23 14:13 UTC
Requires
- php: >=5.3.2
- symfony/framework-bundle: >=2.0.0
This package is auto-updated.
Last update: 2024-09-20 17:22:40 UTC
README
此扩展包是为了在2013年的deSymfony会议上展示如何创建自定义注解而创建的。
关于注解的更多信息,请参阅 Annotations: it’s not a part of my program, but it’s my program。
此扩展包提供了一种简单的方法来禁用操作或控制器。您可以直接禁用,在指定日期/时间后禁用,直到指定日期/时间,或者通过日期/时间范围禁用。您还可以显示禁用消息或将请求重定向到另一个路由。
安装
步骤 1:安装供应商
Symfony 2.0.x: 使用 bin/vendors.php
方法
如果您使用 bin/vendors.php
方法来管理您的供应商库,请将以下条目添加到项目根目录中的 deps
[FerrandiniDisableBundle]
git=http://github.com/aferrandini/DisableBundle.git
target=/bundles/Ferrandini/Bundle/DisableBundle
接下来,运行以下命令更新您的供应商
$ ./bin/vendors
最后,将以下条目添加到您的自动加载器中
<?php // app/autoload.php $loader->registerNamespaces(array( // ... 'Ferrandini' => __DIR__.'/../vendor/bundles', ));
Symfony >=2.1.x: Composer
Composer 是一个PHP项目依赖管理器。您必须在一个 composer.json
文件中列出您的依赖项
{ "require": { "aferrandini/disable-bundle": "dev-master" } }
要实际上安装 DisableBundle 到您的项目中,请下载 composer 二进制文件并运行它
wget https://getcomposer.org.cn/composer.phar
# or
curl -O https://getcomposer.org.cn/composer.phar
php composer.phar install
步骤 2:启用扩展包
最后,在内核中启用扩展包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Ferrandini\Bundle\DisableBundle\FerrandiniDisableBundle(), ); }
用法
如以下示例所示,此扩展包提供了一种简单的方法来禁用控制器或操作。
禁用控制器
<?php namespace Foo\Bundle\FooBundle\Controller; use Ferrandini\Bundle\DisableBundle\Annotations\Disable; /** * @Disable() */ class FooController { }
禁用操作
<?php namespace Foo\Bundle\FooBundle\Controller; use Ferrandini\Bundle\DisableBundle\Annotations\Disable; class FooController { /** * @Disable() */ public function fooAction() { // ... } }
使用自定义消息禁用
<?php namespace Foo\Bundle\FooBundle\Controller; use Ferrandini\Bundle\DisableBundle\Annotations\Disable; /** * @Disable(message="This controller has been disabled with DisableBundle") */ class FooController { }
按日期/时间禁用
日期/时间必须定义为PHP支持的日期和时间格式。您可以在 支持的日期和时间格式 中查看支持的格式
直到指定日期/时间禁用
<?php namespace Foo\Bundle\FooBundle\Controller; use Ferrandini\Bundle\DisableBundle\Annotations\Disable; /** * @Disable(until="2013-11-11 11:11") */ class FooController { }
在指定日期/时间后禁用
<?php namespace Foo\Bundle\FooBundle\Controller; use Ferrandini\Bundle\DisableBundle\Annotations\Disable; /** * @Disable(after="2013-11-11 11:11") */ class FooController { }
按日期/时间范围禁用
<?php namespace Foo\Bundle\FooBundle\Controller; use Ferrandini\Bundle\DisableBundle\Annotations\Disable; /** * @Disable(until="2013-06-11", after="2013-11-11") */ class FooController { }
禁用并重定向到路由
该路由应该是路由配置中定义的路由名称。
<?php namespace Foo\Bundle\FooBundle\Controller; use Ferrandini\Bundle\DisableBundle\Annotations\Disable; /** * @Disable(redirect="_welcome") */ class FooController { }
使用自定义响应状态码禁用
<?php namespace Foo\Bundle\FooBundle\Controller; use Ferrandini\Bundle\DisableBundle\Annotations\Disable; /** * @Disable(statusCode=404) */ class FooController { }