Reputation: 45
Here's my Controller A:
<?php
namespace MonitoringBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Doctrine\ORM\Query\ResultSetMapping;
use MonitoringBundle\Entity\MarketplaceShop;
use MonitoringBundle\Controller\BController;
class AController extends Controller
{
/**
* @Route("/A")
* @Template()
*/
public function AAction()
{
$B = new BController;
$response = $B->BAction();
return $this->render('MonitoringBundle:Default:index.html.twig', array('BVar' => $response));
}
}
?>
And thats Controller B:
<?php
namespace MonitoringBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Doctrine\ORM\Query\ResultSetMapping;
use MonitoringBundle\Entity\MarketplaceShop;
class BController extends Controller
{
/**
* @Route("/B")
* @Template()
*/
public function BAction()
{
$id = 'A2WPX7PK44TEBQ';
$em = $this->getDoctrine()->getManager();
$shop = $em->getRepository('MonitoringBundle:MarketplaceShop')
->findOneByUniqueShopId($id);
if (!$shop) {
// do something
return new Response('Shop does not exist.');
} else {
// do something else
return new Response('Shop exists!');
}
}
}
?>
When I call http://example.com/B, all is fine and I get a response:
Shop does not exist.
But when I call http://example.com/A, I get an error:
Error: Call to a member function has() on a non-object 500 Internal Server Error - FatalErrorException
Stack Trace: in vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php at line 291
public function getDoctrine()
{
if (!$this->container->has('doctrine')) {
throw new \LogicException('The DoctrineBundle is not registered in your application.');
}
Why does it work with /B/ but not with /A/?
Upvotes: 1
Views: 2775
Reputation: 5542
In addition to the @Christian Bujoreau answer, you can use forward method: http://symfony.com/doc/current/book/controller.html#forwarding-to-another-controller
public function AAction()
{
$response = $this->forward('MonitoringBundle:BController:BAction', array(
// 'some_variable' => $some_variable,
));
...
return $response;
}
Upvotes: 0
Reputation: 1167
A solution is to declare your B controller as service and call it in A controller through $this->container->get('controllerB');
In services.yml:
controllerB:
class: MonitoringBundle\Controller\BController
arguments:
entity_manager: "@doctrine.orm.entity_manager"
class AController extends Controller
{
/**
* @Route("/A")
* @Template()
*/
public function AAction()
{
$B = $this->container->get('BController');
$response = $B->BAction();
return $this->render('MonitoringBundle:Default:index.html.twig', array('BVar' => $response));
}
}
class BController extends Controller {
/**
* @var EntityManager
*/
protected $entityManager;
/**
* Constructor
* @param $entityManager
*/
public function __construct($entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @Route("/B")
* @Template()
*/
public function BAction()
{
$id = 'A2WPX7PK44TEBQ';
$shop = $this->entityManager->getRepository('MonitoringBundle:MarketplaceShop')
->findOneByUniqueShopId($id);
if (!$shop) {
// do something
return new Response('Shop does not exist.');
} else {
// do something else
return new Response('Shop exists!');
}
}
}
Upvotes: 1