Symfony2 data transfer between packages and controllers

it is more a question of "best practice" than a real problem:

I am working on a project in Symfony 2, and I created a package to handle all of my web services. Basically, one controller takes some JSON data, sends it to another controller to check if it matches the specified format, then it turns off to another controller to process database calls and ultimately returns to the original controller to return a JSON response.

I am sending data between controllers, doing something like this:

$controller = new \Acme\myBundle\Controller\DefaultController;
$response = $controller->function();

This works correctly, but I always run into one problem. In the controller, I pass data so that I need to create an instance of AppKernel and call the load function for any Symfony function. It seems a little silly to me, which makes me believe that I can do it all wrong.

Any suggestions or tips appreciated!

EDIT / UPDATE Thank you all for your comments. I configured my controllers as services, the services work, but I still need to manually load / create kernel instances when I call the service from inside the service.

#config.yml
# API Services
services:
service.handler:
    class: Acme\APIBundle\Controller\ServicesController
    arguments:
        container: "@service_container"

service.definitions:
    class: Acme\APIBundle\Controller\DefinitionController
    arguments:
        container: "@service_container"

From another bundle controller, I can easily call a function from any of these services:

 $test = $this->get("service.handler")->testFunction();
 $test2 = $this->get("service.definitions")->anotherTestFunction();

If I have a problem, if I call a function inside one service and then try to call another service from inside this service, I get the following PHP error

Fatal error: Call to a member function get() on a non-object in /vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php on line 188

, , $this

    public function bootKernel(){
//boot kernel
    $controller = new \Acme\myBundle\Controller\DefaultController; 
    $kernel = new \AppKernel('dev', true);$kernel->loadClassCache(); 
    $kernel->boot(); 
    $controller->setContainer($kernel->getContainer());
    return($controller);    
}

, "", , , -.

2: , , , ... , -.

    protected $container;

public function __construct($container) {
    $this->container= $container;
}
+5
2

controller-as-a-service . , 3 . foo(), bar() something(). :

namespace Acme\DemoBundle\Controller;

class FooController
{
    public function __construct($container, ...)
    {
        $this->container = $container;
        // ... deal with any more arguments etc here
    }

    public function foo($params)
    {
        // ...
        return $x;
    }

    protected function get($service)
    {
        return $this->container->get($service);
    }
}

bar() something(), . . , config.yml ( ):

services:
    my.foo.service:
        class: Acme\FooBundle\Controller\FooController
        arguments:
            container: "@service_container"

. Symfony , , , . :

public function yourMainAction()
{
    // ...

    $newData = $this->get("my.foo.service")->fooAction($someData);

    // ...

    return new Response(json_encode($newData), ...);
}

BarController SomethingController. , ( , ), , .

.. Symfony (. ).

. , . get() .

+10
+1

All Articles