symfony - 检索服务中的参数 - Symfony

标签 symfony

我想了解 symfony 中的服务

我已阅读http://symfony.com/doc/2.3/book/service_container.html#creating-configuring-services-in-the-container

我想尝试创建我的服务

在我的 config.yml 中

services:
    my_service:
        class:        Acme\FooBundle\myService
        arguments:    ['my_param']

在我的 Controller 中

public function serviceAction(){
        $Myservice = $this->get('my_service');
        return array();
    }

在我的类(class)/服务

class myService{

    public function __construct() {
        echo "In BaseClass constructor\n";
    }

} 

问题是: 如何在我的 Class 服务中检索 my_param? 我想检索参数:['my_param']

最佳答案

参数正在作为参数传递给您的服务构造函数:

class myService{

    private $my_param;

    public function __construct($param) {
        $this->my_param = $param;
        echo $this->my_param;
        echo "In BaseClass constructor\n";
    }

}

关于symfony - 检索服务中的参数 - Symfony,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26665117/

相关文章:

php - Symfony - 在更新过程中找不到该文件

symfony - 依赖不存在的服务 "doctrine.orm.default_entity_manager"

php - 使用查询构建器删除两个表之间的链接

symfony - 验证不存在于表单中但存在于实体中的字段

symfony - SonataMediaBundle 和 Symfony 4 中的查看和下载路线被拒绝访问

php - 交响乐 4 : Override public services in container

symfony - 使用 Symfony2 通过 API 使用用户密码进行身份验证

symfony - 如何处理 Sonata AdminBundle ListView 中的每个实体?

javascript - javascript、jquery在Symfony2中是如何工作的

Symfony 安全包 : how to stay on the same page after logout?