2
votes

I am learning Symfony2 and how to do dependency injection. The question might be a bit long one but i guess I have to include all the code that leads to this error.

So i have a Factory design pattern in my Symfony2 project Interfacr -> Factory -> Controller.

My Interface file:

<?php
namespace Inter\DemoBundle\Interfaces;

interface ObjectInterface {

    public function create($testObject);

}

Factory File:

<?php
namespace Inter\DemoBundle\Factory;
use Inter\DemoBundle\Interfaces\ObjectInterface;

class ObjectFactory implements ObjectInterface{

    public function create($testObject)
    {
        $testObject = 'Hello World';
        return $testObject;
    }
}

Controller File:

<?php
namespace Inter\DemoBundle\Controller\DataController;

use Inter\DemoBundle\Factory\ObjectFactory;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * @Route("/test" service="inter.controller")
 */
class DataController
{
    public $object;

    public function __construct(
        ObjectFactory $objectFactory
    ){
        $this->object = $objectFactory;
    }

    public function test()
    {
        return $this->object;
    }
}

Services File:

 <?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <!--Factory Services-->
        <service id="inter.factory.object.factory"
                 class="Inter\DemoBundle\Factory\ObjectFactory">
        </service>
        <!--Controller Services-->
        <service id="inter_controller" class="Inter\DemoBundle\DataController">
            <argument type="service" id="inter.factory.object.factory" />
        </service>
    </services>
</container>

Error:

FileLoaderImportCircularReferenceException in FileLoader.php line 97:
Circular reference detected in "/home/tomazi/Dev/interface.test/app/config/routing_dev.yml" 

("/home/tomazi/Dev/interface.test/app/config/routing_dev.yml" > "/home/tomazi/Dev/interface.test/app/config/routing.yml" >

"/home/tomazi/Dev/interface.test/src/Inter/DemoBundle/Controller/" > "/home/tomazi/Dev/interface.test/app/config/routing_dev.yml").

I tried tried to fix this for ages now but no luck at all i Google it and now answers there ather than people complaining this is a miss leading Error in smymfony2 which might have nothing to do with routing.

Can someone help here please

1
could you also add rounting and rounting_devb.b3rn4rd

1 Answers

2
votes

You forgot to add a comma , in your annotation.

/**
 * @Route("/test" service="inter.controller")
 */

should be:

/**
 * @Route("/test", service="inter.controller")
 */

Also, bear in mind that you might be typed wrong namespace here:

<service id="inter_controller" class="Inter\DemoBundle\DataController">

This is your controller definition right now:

<?php
namespace Inter\DemoBundle\Controller\DataController;

class DataController {}

-Update-

Please, double check everything you've wrote, there must be some logic mistake. I've tried to reproduce your problem, but everything works fine.

services.yml

services:
    test.service:
        class: AppBundle\Service\TestService

    service.controller:
        class: AppBundle\Controller\ServiceController
        arguments: 
            - @test.service

TestService.php

namespace AppBundle\Service;

class TestService {

    private $property = 'test';

}

ServiceController.php

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/**
 * @Route("/test", service="service.controller")
 */
class ServiceController {

    private $object;

    public function __construct($service) {
        $this->object = $service;
    }

    /**
     * @Route("/testing")
     **/
    public function test() {
        var_dump($this->object);

        exit;
    }

}

Accessing /app_dev.php/test/testing gives the following output(dump):

object(AppBundle\Service\TestService)[2215]
  private 'property' => string 'test' (length=4)

as expected. So, check your code again.