Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7598111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:19:36+00:00 2026-05-30T22:19:36+00:00

this is more of a ‘best practice’ question than an actual problem: I’m working

  • 0

this is more of a ‘best practice’ question than an actual problem:

I’m working on a project in Symfony 2 and I’ve built a bundle to handle all of my webservices. Basically one controller takes some JSON data, sends it off to another controller to check it matches a described format, then off to another controller to handle the database calls and eventually back to the initial controller to return a JSON response.

I’m sending data between controllers by doing something like this:

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

This works correctly, but I keep coming across one problem. In the controller I pass data to I need to instantiate AppKernel and call the boot function for any Symfony function to work. This to me seems a little bit silly which leads me to believe I may be doing this all wrong.

Any suggestions or tips appreciated!

EDIT/UPDATE
Thank you all for the comments. I have set up my controllers as services, the services are working but I am still needing to manually boot/instantiate the kernel when I call a service from inside a 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 bundles controller I can then call a function from either of those services without problem:

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

Where I DO have a problem is if I call a function inside of one service, then try to call another service from inside that 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

I can avoid that error by using this function and calling it rather than using $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);    
}

I guess my solution ‘works’ but it certainly doesn’t seem an efficient way to do things.

EDIT 2:
If I stick this at the top of the class then modify the service call it seems to work… Still not sure if this is the best way to do things.

    protected $container;

public function __construct($container) {
    $this->container= $container;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:19:37+00:00Added an answer on May 30, 2026 at 10:19 pm

    I’d go down the controller-as-a-service route with the above. Say you have to call 3 methods as part of your processing. Let’s call them foo(), bar() and something(). Each of these methods are in separate controllers:

    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);
        }
    }
    

    Ditto for the bar() and something() methods, each in their own controller. Then, add them to your application as a service. Eg in your config.yml (other methods available):

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

    See the Symfony docs for more details about how you can construct this entry, including injecting any dependencies such as an entity manager or similar. Now, you can get an instance of this from the container:

    public function yourMainAction()
    {
        // ...
    
        $newData = $this->get("my.foo.service")->fooAction($someData);
    
        // ...
    
        return new Response(json_encode($newData), ...);
    }
    

    Likewise again for BarController and SomethingController. The advantage now is that this service can be made available at any point in your application (whether via the container as above, or as an injected service), across bundles, without needing to instantiate the class manually yourself and provide any dependencies.

    For more information about the container etc, the Symfony docs have a good section on it (referenced in the link above).

    Edit: Adjusted the code example to include details of passing in arguments into the service. Also added a convenience get() method to retrieve services from the container.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This more of a style question, rather than a how to. So I've got
This is more of a design question. I am working on a website and
This is more of an academic inquiry than a practical question. Are there any
Ok, I'm going to try to make this more clear because my last question
This is more of a specific question on a specific case. Here is what
This is more of an generic XML Schema question, but if and how do
I have an interesting situation here this time. This more of a conceptual question,
A quick question involving PHP development, I seem to be wondering about this more
I might be making this more complicated than I have to. I have a
During development I've seen xml read errors like this more than once: TestData.ReadFromXml: xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.