2013-09-04 19 views
5

Tôi đang cố gắng nối vào các sự kiện giao diện điều khiển của symfonys với phiên bản tiêu chuẩn của symfony (2.3), nhưng nó sẽ không hoạt động.Làm cách nào để nghe các sự kiện trong symfony?

Tôi tạo ra một người biết lắng nghe theo example của họ và làm theo guides on event registration:

namespace Acme\DemoBundle\EventListener; 

use Symfony\Component\Console\Event\ConsoleCommandEvent; 
use Symfony\Component\Console\ConsoleEvents; 

class AcmeCommandListener 
{ 
    public function onConsoleCommand(ConsoleCommandEvent $event) { 
     // get the output instance 
     $output = $event->getOutput(); 

     // get the command to be executed 
     $command = $event->getCommand(); 

     // write something about the command 
     $output->writeln(sprintf('Before running command <info>%s</info>', $command->getName())); 
    } 
} 

và ai đó trong danh sách gửi thư nói với tôi để đăng ký nó như sự kiện trong container dịch vụ. Vì vậy, tôi đã làm điều này:

services: 
    kernel.listener.command_dispatch: 
     class: Acme\DemoBundle\EventListener\AcmeCommandListener 
     tags: 
      - { name: kernel.event_listener, event: console.command } 

Nhưng rõ ràng việc gắn thẻ không đúng và tôi không thể tìm thấy tên chính xác cho điều đó. Tôi sẽ làm như thế nào?

+0

Bạn đang sử dụng 'ứng dụng php/bảng điều khiển' hoặc ứng dụng tùy chỉnh? – Touki

+0

Tôi đang sử dụng phiên bản chuẩn của Symfony và muốn thêm sự kiện kích hoạt trước khi lệnh 'app/console' được thực hiện. Tôi đã yêu cầu trong nhóm symfony google và ai đó nói rằng tôi cần phải cấu hình nó bằng cách sử dụng một dịch vụ (như tôi đã thử ở trên). – acme

Trả lời

1

Vì vậy, cuối cùng tôi đã nhận được nó. Các mã trên trong bài gốc ist làm việc đầy đủ, nhưng tôi đã xác định services.yml của tôi trong gói của tôi không có trong cấu hình ứng dụng app/config.yml. Điều này có nghĩa là cấu hình chưa bao giờ được tải. Tôi đã phải nhập khẩu các cấu hình thông qua phần mở rộng container:

# Acme/DemoBundle/DependencyInjection/AcmeDemoExtension.php 
namespace Acme\DemoBundle\DependencyInjection; 

use Symfony\Component\DependencyInjection\ContainerBuilder; 
use Symfony\Component\Config\FileLocator; 
use Symfony\Component\HttpKernel\DependencyInjection\Extension; 
use Symfony\Component\DependencyInjection\Loader; 

class AcmeDemoExtension extends Extension 
{ 
    public function load(array $configs, ContainerBuilder $container) 
    { 
     $configuration = new Configuration(); 
     $config = $this->processConfiguration($configuration, $configs); 

     $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 
     $loader->load('services.yml'); 
    } 
} 

# Acme/DemoBundle/DependencyInjection/Configuration.php 
namespace Acme\DemoBundle\DependencyInjection; 

use Symfony\Component\Config\Definition\Builder\TreeBuilder; 
use Symfony\Component\Config\Definition\ConfigurationInterface; 

class Configuration implements ConfigurationInterface 
{ 
    public function getConfigTreeBuilder() 
    { 
     $treeBuilder = new TreeBuilder(); 
     $rootNode = $treeBuilder->root('headwork_legacy'); 
     return $treeBuilder; 
    } 
} 

Mặc dù tôi đoán bạn thậm chí có thể bỏ qua phần $configuration = new Configuration(); và lớp Configuration.