English 中文(简体)
B. 在 Sym缩的简单杂货舱内使用集装箱 2
原标题:Using the container inside a simple bundle class in Symfony 2
  • 时间:2012-04-26 22:25:01
  •  标签:
  • php
  • symfony

I have created a simple class inside my bundle in Symfony 2:

class MyTest {
    public function myFunction() {
        $logger = $this->get( logger );
        $logger->err( testing out );
    }
}

如何进入集装箱?

最佳回答

你们需要注入服务集装箱。 你的班子将研究:

use SymfonyComponentDependencyInjectionContainerInterface;

class MyTest
{
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function myFunction()
    {
        $logger = $this->container->get( logger );
        $logger->err( testing out );
    }
}

然后在控制器或集装箱内 意识到:

$myinstance = new MyTest($this->container);

如果需要更多的解释:

问题回答

在大多数情况下,对整个集装箱进行拦截是坏的。 单独拒绝所需服务。

namespace Vendor;

use SymfonyComponentHttpKernelLogLoggerInterface;

class MyTest 
{
    private $logger;

    public function __construct(LoggerInterface $logger) 
    {
        $this->logger = $logger;
    }

    public function myFunction() 
    {
        $logger->err( testing out );
    }
}

登记<代码>服务:yml:

services:
    my_test:
        class: VendorMyTest
        arguments: [@logger]

为什么不添加<条码>@logger 只是服务? e.g

arguments: [@logger]

如果你想添加集装箱(WHICH IS NOT RecommendationsED),那么你可以在<代码>@service_container上添加<条码>。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签