1

I have strange problem with symfony2. In service.yml I declared pagination service:

site.paginate_service:
class: "Smestaj\SiteBundle\Services\PaginationService"
arguments:
  - "@service_container"

Service looks like this:

namespace Smestaj\SiteBundle\Services;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PaginationService{

protected $cn;
protected $numberOfData;

public function __construct(ContainerInterface $container)
{
    $this->cn = $container;
    $this->numberOfData = $container->getParameter("limit")['adsPerPage'];
}

Problem is when I call this service in service.yml into another service as dependacy injection

site.ads_service:
class: "Smestaj\SiteBundle\Services\AdsService"
arguments:
  - "@doctrine.orm.entity_manager"
  - "@service_container"
calls:
  - [setPaginate, ["@site.paginate_service"]]

then I get this error message:

Attempted to load class "Services
aginationService" from namespace "Smestaj\SiteBundle". Did you forget a "use" statement for another namespace?

So, from this message it's clearly that symfony trying to call class "ServicesaginationService". My class has Smestaj\SiteBundle\Services\PaginationService. Symfony, somehow merge Services and PaginationService name, and remove "P" from name.

If I Change Class name to AaaService then everything works fine.

2
  • Try to remove quotes from class values. Commented Oct 3, 2016 at 7:31
  • Its a bad idea to inject service_container to service itself. You should inject only dependant services, parameters, etc., but NOT service container Commented Oct 3, 2016 at 11:02

1 Answer 1

1

When you use double-quotes in class name in service.yml, you have to escape the \ by adding another \:

class: "Smestaj\\SiteBundle\\Services\\PaginationService"

But the best way to avoid problems is to remove quotes, as the path will be corectly interpreted as string by the YML parser:

class: Smestaj\SiteBundle\Services\PaginationService
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response. Your answer solved my problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.