简体   繁体   English

无法在我的控制器symfony2中获得服务

[英]Cannot get a service in my controller symfony2

I have a problem in my controller when I want to get a sevice that I create, everything work well when I'm in localhost, but when I put the website in my server I get the problem. 当我想要获得我创建的服务时,我的控制器出现问题,当我在localhost时一切正常,但是当我将网站放入我的服务器时,我遇到了问题。

Here is the line that make the error in my controller: 以下是在我的控制器中出错的行:

$myService = $this->get('Acme_test.service');

The services.yml : services.yml

services:
    Acme_test.service:
        class: Acme\testBundle\Services\Testservice
        arguments: [%folder%, @service_container]

And the error: 而错误:

ClassNotFoundException: Attempted to load class "Testservice" from namespace "Acme\TestBundle\Services" in /home/www/acme/app/cache/dev/appDevDebugProjectContainer.php line 736. Do you need to "use" it from another namespace?

I tried to clear the cache, still having the same error !! 我试图清除缓存,仍然有同样的错误!!

You need to set nested your service to the services node in the services.yml file like: 您需要将服务的嵌套设置为services.yml文件中的services节点,如:

services:
    acme_test.service:
        class: Acme\TestBundle\Service\TestService
        arguments: [%folder%, @service_container]

And I think better write service name in lowercase. 我认为最好用小写写服务名称。 And then get service in controller like: 然后在控制器中获得服务,如:

$myService = $this->get('acme_test.service');

NOTE: I think that you meant TestBundle and TestService names in camelCase 注:我认为你的意思是TestBundleTestService 首字母大写的名字

NOTE2: And commonly services dir name is Service , so check your namespace, it need to be Acme\\TestBundle\\Service , class name need to be TestService and file Acme\\TestBundle\\Service\\TestService.php must exists. 注意2:并且通常服务目录名称是Service ,所以检查你的命名空间,它需要是Acme\\TestBundle\\Service ,类名需要是TestService而文件Acme\\TestBundle\\Service\\TestService.php必须存在。

NOTE3: After all recommendations and modifications manually clear cache dir. 注3:手动清除缓存目录后,所有建议和修改完成后。

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

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