简体   繁体   English

在Symfony2中充当服务

[英]Function as service in Symfony2

I am storing a string in database that I want to access from various places in my application. 我正在数据库中存储一个字符串,我想从我的应用程序的各个位置访问该字符串。 I figure out that the best solution will be create a function that is taking that string from database and register it as a service. 我发现最好的解决方案是创建一个函数,该函数从数据库中获取该字符串并将其注册为服务。

Function: 功能:

public function shopUrlAction()
{ 
  return new Response($this->getDoctrine()->getRepository('AppBundle:Settings')->find(1)->getName());
}

service.yml service.yml

services:
  app.default_controller:
    class: AppBundle\Controller\DefaultController

output in other controller: 在其他控制器中输出:

$return['base_url'] = $this->forward('app.default_controller:shopUrlAction');

Unfortunately I am constantly getting 不幸的是我不断

CRITICAL - Uncaught PHP Exception Symfony\\Component\\DependencyInjection\\Exception\\ServiceNotFoundException: "You have requested a non-existent service "app.default_controller"." 严重-未捕获的PHP异常Symfony \\ Component \\ DependencyInjection \\ Exception \\ ServiceNotFoundException:“您请求的服务不存在“ app.default_controller”。 at /app/bootstrap.php.cache line 2099 Context: {"exception":"Object(Symfony\\Component\\DependencyInjection\\Exception\\ServiceNotFoundException)"} 在/app/bootstrap.php.cache第2099行中上下文:{“ exception”:“ Object(Symfony \\ Component \\ DependencyInjection \\ Exception \\ ServiceNotFoundException)”}

I've cleared cache. 我已经清除了缓存。

As I see from your question you have service.yml instead of services.yml (in plural form). 从您的问题中可以看出,您使用的是service.yml而不是services.yml (复数形式)。

You should include your service.yml in main config.yml in imports section or use standard path to it ( AppBundle/Resources/config/services.yml ) 您应该在imports部分的主config.yml中包含service.yml或使用标准路径( AppBundle/Resources/config/services.yml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM