1

I got two Controllers:

class FirstController extends Controller
{
    /**
     * @Route("/first")
     */
    public function indexAction()
    {
        $second = new SecondController();
        $second->doit();
    }
}


class SecondController extends Controller
{
    public function doit()
    {
        $render = $this->renderView('::base.html.twig');
        //write $render to disk
    }
}

Instead of 'done' being written to my screen, I got error: Error: Call to a member functuin get() on null

What am I doing wrong?

1 Answer 1

2

You are not supposed to instantiate controllers yourself. The framework is supposed to do that for you. If you are in controller1, and want controller2 to actually handle the request, which could use some explanation on your part, you need to forward to it, like this : $this->forward('MyBundle:MyController:myaction)

Also, you should maybe have your doit method in a service. Controllers are supposed to stay skinny, and care only about HTTP:

  • they receive the request
  • they call a service based on the request, this service should know nothing about HTTP, and be callable from a command as well as from a controller
  • they return a response based on what the service answers.
Sign up to request clarification or add additional context in comments.

2 Comments

and you should use the Action prefix e.g. indexAction()
I am not sure the thing he calls even is an action, that's also why I'm suggesting to use a service.

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.