【发布时间】:2015-08-26 09:36:37
【问题描述】:
这是我的控制器 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));
}
}
?>
那是控制器 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!');
}
}
}
?>
当我拨打http://example.com/B 时,一切都很好,我得到了回复:
商店不存在。
但是当我调用http://example.com/A 时,我得到一个错误:
错误:在非对象上调用成员函数 has() 500 内部服务器错误 - FatalErrorException
堆栈跟踪: 在 vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php 第 291 行
public function getDoctrine()
{
if (!$this->container->has('doctrine')) {
throw new \LogicException('The DoctrineBundle is not registered in your application.');
}
为什么它适用于 /B/ 而不适用于 /A/?
【问题讨论】:
标签: php symfony controller doctrine