我刚刚使用DefaultBundle
生成了一个包(Manufacturer
),一个实体(php bin/console generate:doctrine:crud --entity=DefaultBundle:Manufacturer
)及其CRUD控制器。
问题:
当我尝试向生成的控制器添加新操作时,如下所示:
/**
* @Route("/search", name="manufacturer_search")
* @Method({"GET"})
*/
public function searchAction()
{
return new Response("foobar");
}
并请求app_dev.php/manufacturer/search
,我收到此错误:
找不到DefaultBundle \ Entity \ Manufacturer对象。 404未找到 - NotFoundHttpException
这是空心控制器:
?php
namespace DefaultBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use DefaultBundle\Entity\Manufacturer;
use DefaultBundle\Form\ManufacturerType;
/**
* Manufacturer controller.
*
* @Route("/manufacturer")
*/
class ManufacturerController extends Controller
{
/**
* Lists all Manufacturer entities.
*
* @Route("/", name="manufacturer_index")
* @Method("GET")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$manufacturers = $em->getRepository('DefaultBundle:Manufacturer')->findAll();
return $this->render('manufacturer/index.html.twig', array(
'manufacturers' => $manufacturers,
));
}
/**
* Creates a new Manufacturer entity.
*
* @Route("/new", name="manufacturer_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$manufacturer = new Manufacturer();
$form = $this->createForm('DefaultBundle\Form\ManufacturerType', $manufacturer);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($manufacturer);
$em->flush();
return $this->redirectToRoute('manufacturer_show', array('id' => $manufacturer->getId()));
}
return $this->render('manufacturer/new.html.twig', array(
'manufacturer' => $manufacturer,
'form' => $form->createView(),
));
}
/**
* Finds and displays a Manufacturer entity.
*
* @Route("/{id}", name="manufacturer_show")
* @Method("GET")
*/
public function showAction(Manufacturer $manufacturer)
{
$deleteForm = $this->createDeleteForm($manufacturer);
return $this->render('manufacturer/show.html.twig', array(
'manufacturer' => $manufacturer,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing Manufacturer entity.
*
* @Route("/{id}/edit", name="manufacturer_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, Manufacturer $manufacturer)
{
$deleteForm = $this->createDeleteForm($manufacturer);
$editForm = $this->createForm('DefaultBundle\Form\ManufacturerType', $manufacturer);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($manufacturer);
$em->flush();
return $this->redirectToRoute('manufacturer_edit', array('id' => $manufacturer->getId()));
}
return $this->render('manufacturer/edit.html.twig', array(
'manufacturer' => $manufacturer,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a Manufacturer entity.
*
* @Route("/{id}", name="manufacturer_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, Manufacturer $manufacturer)
{
$form = $this->createDeleteForm($manufacturer);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($manufacturer);
$em->flush();
}
return $this->redirectToRoute('manufacturer_index');
}
/**
* Creates a form to delete a Manufacturer entity.
*
* @param Manufacturer $manufacturer The Manufacturer entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(Manufacturer $manufacturer)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('manufacturer_delete', array('id' => $manufacturer->getId())))
->setMethod('DELETE')
->getForm()
;
}
/**
* @Route("/search", name="manufacturer_search")
* @Method({"GET"})
*/
public function searchAction()
{
return new Response("foobar");
}
}
Stack Trace
in var/cache/dev/classes.php at line 7597 -
}
}
if (null === $object && false === $configuration->isOptional()) {
throw new NotFoundHttpException(sprintf('%s object not found.', $class));
}
$request->attributes->set($name, $object);
return true;
at DoctrineParamConverter ->apply (object(Request), object(ParamConverter))
in var/cache/dev/classes.php at line 7777 +
at ParamConverterManager ->applyConverter (object(Request), object(ParamConverter))
in var/cache/dev/classes.php at line 7750 +
at ParamConverterManager ->apply (object(Request), array('manufacturer' => object(ParamConverter)))
in var/cache/dev/classes.php at line 7484 +
at ParamConverterListener ->onKernelController (object(FilterControllerEvent), 'kernel.controller', object(TraceableEventDispatcher))
at call_user_func (array(object(ParamConverterListener), 'onKernelController'), object(FilterControllerEvent), 'kernel.controller', object(TraceableEventDispatcher))
in vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php at line 61 +
at WrappedListener ->__invoke (object(FilterControllerEvent), 'kernel.controller', object(ContainerAwareEventDispatcher))
at call_user_func (object(WrappedListener), object(FilterControllerEvent), 'kernel.controller', object(ContainerAwareEventDispatcher))
in var/cache/dev/classes.php at line 2167 +
at EventDispatcher ->doDispatch (array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.controller', object(FilterControllerEvent))
in var/cache/dev/classes.php at line 2082 +
at EventDispatcher ->dispatch ('kernel.controller', object(FilterControllerEvent))
in vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php at line 136 +
at TraceableEventDispatcher ->dispatch ('kernel.controller', object(FilterControllerEvent))
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php at line 141 +
at HttpKernel ->handleRaw (object(Request), '1')
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php at line 68 +
at HttpKernel ->handle (object(Request), '1', true)
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php at line 169 +
at Kernel ->handle (object(Request))
in web/app_dev.php at line 28 +
Logs -
1 error
INFO - Matched route "{route}".
INFO - Populated the TokenStorage with an anonymous Token.
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - SELECT t0.id AS id_1 FROM manufacturer t0 WHERE t0.id = ?
ERROR - Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "DefaultBundle\Entity\Manufacturer object not found." at /var/www/prueba/var/cache/dev/classes.php line 7597
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
Stack Trace (Plain Text) -
[1] Symfony\Component\HttpKernel\Exception\NotFoundHttpException: DefaultBundle\Entity\Manufacturer object not found.
at n/a
in /var/www/prueba/var/cache/dev/classes.php line 7597
at Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter->apply(object(Request), object(ParamConverter))
in /var/www/prueba/var/cache/dev/classes.php line 7777
at Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterManager->applyConverter(object(Request), object(ParamConverter))
in /var/www/prueba/var/cache/dev/classes.php line 7750
at Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterManager->apply(object(Request), array('manufacturer' => object(ParamConverter)))
in /var/www/prueba/var/cache/dev/classes.php line 7484
at Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener->onKernelController(object(FilterControllerEvent), 'kernel.controller', object(TraceableEventDispatcher))
in line
at call_user_func(array(object(ParamConverterListener), 'onKernelController'), object(FilterControllerEvent), 'kernel.controller', object(TraceableEventDispatcher))
in /var/www/prueba/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php line 61
at Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(object(FilterControllerEvent), 'kernel.controller', object(ContainerAwareEventDispatcher))
in line
at call_user_func(object(WrappedListener), object(FilterControllerEvent), 'kernel.controller', object(ContainerAwareEventDispatcher))
in /var/www/prueba/var/cache/dev/classes.php line 2167
at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.controller', object(FilterControllerEvent))
in /var/www/prueba/var/cache/dev/classes.php line 2082
at Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.controller', object(FilterControllerEvent))
in /var/www/prueba/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php line 136
at Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch('kernel.controller', object(FilterControllerEvent))
in /var/www/prueba/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 141
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
in /var/www/prueba/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 68
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
in /var/www/prueba/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 169
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
in /var/www/prueba/web/app_dev.php line 28
答案 0 :(得分:0)
请求网址不应该是:
app_dev.php/search
从您的代码中看起来就是这样。我不确定这是否正确,但你可以试试吗?
答案 1 :(得分:0)
在symfony的调试工具栏中,我注意到请求的路由是manufacturer_show
。所以我刚刚删除了相应的操作,因为我没有使用它,现在当我请求我在我的问题(manufacturer/search
)中提到的URL时,我到达了刚刚创建的操作(searchAction
)
答案 2 :(得分:0)
您的搜索操作需要在您的展示操作之前进行。 Symfony按顺序编译路线。因为您的展示模式"/{id}"
基本上匹配斜杠"/search"
之后的任何内容,例如,您的搜索方法将无法访问。
但是,如果您将搜索方法移到show方法之上并清除缓存,则路由将按预期工作。
请注意,这也意味着您可以拥有任何" id"&,其值为"搜索"或者他们不匹配