knplabs/rad-prototype

此包已被废弃且不再维护。未建议替代包。

Pierre PLAZANET <pierre@knplabs.com>

v2.1.0 2017-09-21 08:23 UTC

This package is auto-updated.

Last update: 2022-09-23 13:36:32 UTC


README

遗憾的是,我们决定不再维护此项目(查看原因)。如果您想将其他包作为此包的替代品,请发送电子邮件至 hello@knplabs.com

快速应用程序开发:原型

自动将方法注入对象

Build Status Scrutinizer Code Quality Latest Stable Version Total Downloads Latest Unstable Version License

安装

composer require knplabs/rad-prototype ~2.0
class AppKernel
{
    function registerBundles()
    {
        $bundles = array(
            //...
            new Knp\Rad\Prototype\Bundle\PrototypeBundle(),
            //...
        );

        //...

        return $bundles;
    }
}

用法

1. 方法端

您可以通过依赖注入创建可注入的方法。您可以应用标签 knp_rad_prototype.prototype_method。例如,如果我想公开来自 @doctrine 服务的 getRepository 方法,我只需要声明一个新的方法

knp_rad_prototype.prototype.method.doctrine.get_repository:
    class: Knp\Rad\Prototype\Prototype\Method
    arguments:
       - @doctrine
       - getRepository
   tags:
       - { name: knp_rad_prototype.prototype_method, alias: getRepository, domain: doctrine }

第一个参数应该是服务或类名(对于静态方法),第二个参数应该是方法名。在这种情况下,该方法将是 @doctrine->getRepository()

alias 标签选项代表原型中的方法名,所以在这种情况下,我可以使用 $this->getRepository() 调用该方法。最后,domain 标签选项代表此方法的作用域,一个原型可以接收来自某些作用域的方法。

2. 原型端

2.1. 将方法附加到控制器

从控制器访问方法

您的控制器应实现 Knp\Rad\Prototype\Prototype 接口。您也可以通过使用特质 Knp\Rad\Prototype\Prototype\Controller 来使用基本实现

namespace AppBundle\Controller;

use Knp\Rad\Prototype\Prototype;

class ProductController implements Prototype
{
    use Prototype\Controller;

    public function testAction($id) 
    {
        $product = $this->getRepository('AppBundle:Product')->find($id);

        $this->persist($product);
        $this->flush();

        // ...
    }
}

3. Web Debug Toolbar

当您的控制器实现 Knp\Rad\Prototype\Prototype 接口时,以下图标将出现在 Symfony Web Debug Toolbar 中。

wdt.png

您还可以从 Symfony Profiler 界面访问来自您的原型的每个可访问方法

profiler.png