Symfony - 访问变量内部服务

时间:2017-10-15 19:42:36

标签: symfony service

我希望访问服务中的变量。 变量是一个对象类。

这是services.yml

services: 
    project.notification:
        class: NotificationsBundle\Command\ServerCommand 

//这是班级

class ServerCommand extends ContainerAwareCommand {

    public $notification;
    /**
     * Configure a new Command Line
     */
    protected function configure() { 
        $this->setName('Project:notification:server') ->setDescription('Start the notification server.');

    }

    public function getNotification()
    {  
        return $this->notification;
    }

    protected function execute(InputInterface $input, OutputInterface $output) {

        $this->notification = new Notification();
        $server = IoServer::factory(new HttpServer(
            new WsServer(
                $this->notification
            )
        ), 8081);

        $server->loop->addPeriodicTimer(1, function ()  {  
            $this->notification->sendToAll('Hello'); 
        }); 

        $server->run();

    }

}

我想从另一个控制器获取变量$ notification。 当我这样做时,我得到一个错误"不存在的对象" ($通知)。

我通过执行以下命令来运行服务:

php app/console Project:notification:server

它必须是我无法创建新对象的当前对象,因为它位于对象$ notification中的用户列表。

有什么想法吗?

0 个答案:

没有答案
相关问题