0

I have two controllers

DefaultController

class DefaultController extends Controller
{
    public function indexAction()
    {

ApiController

class ApiController extends Controller
{
    public function getCategoryAction()
    {

Now I want to call getCategoryAction from my DefaultController.

Is it impossible or how can I make it?

4
  • What do you mean by "call" getCategoryAction. Do you just want to return the same response/template or process the returned data further afterwards ? Commented Feb 18, 2014 at 1:29
  • getCategoryAction shows categoryList.xml ( because this controller is for API usage). So,I want to use categoryList.xml in indexAction() Commented Feb 18, 2014 at 3:02
  • can you be more specific on what you mean in terms of "use it" ? do you want to do something with it (like read it and change something) or just return the same xml as the API action ? Commented Feb 18, 2014 at 3:12
  • I would like to parse the xml and use data in html. Commented Feb 18, 2014 at 9:20

4 Answers 4

1

There is forwarding in Symfony2. So, you could do sth like that:

class DefaultController extends Controller
{
    public function indexAction()
    {
        // @var Response $categoryListResponse
        $categoryListResponse = $this->forward('YourBundle:Api:getCategory');
        // ... further modify the response or return it directly
        return $categoryListResponse;
    }
}

Where $categoryListResponse is Response type and represents an HTTP response. So you could $categoryListResponse->getContent() from this response.

Sign up to request clarification or add additional context in comments.

Comments

0

You can configure controller as a service and then use

$this->forward('controller.service.name')->methodOnTheController()

Comments

0

you can create you own services and then call them from all the controllers

see Symfony service container

Comments

0

You can extend ApController controller like this:

class ApiController extends DefaultController
{
    public function yourAction()
    {
         $this->getCategoryAction();
    }
}

Or even better create a MyClass class (if getCategoryAction is going to be reused) register as a service

It all depend what you want to achieve at the end

Comments

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.