drwf69817 2015-02-19 12:54
浏览 35

symfony 2:在控制器内调用动作

i'm working on for symfony 2 framework, and i have a problem by invoke a custom Action inside Controller. I've connected my app to a database (on xampp), and for each table i have Entity class. Now i've generate a CRUD controller (with doctrine) for one of this entity (GuestController.php), and i need to create a new Action inside it, and invoke it. this is action code:

 /**
 *
 * @Route("/", name="my_action")
 * 
 */
public function customAction() {
    return new Response('<html><body>Hello</body></html>');
}

Now if i try to invoke it with this link

http://localhost/TEST/web/app_dev.php/guest/guest_search_by_saloon

i obtain

Unable to find Guest entity.

what's wrong?

This is full controller class

/**
 * Guest controller.
 *
 * @Route("/guest")
 */
class GuestController extends Controller {

/**
 * Lists all Guest entities.
 *
 * @Route("/", name="guest")
 * @Method("GET")
 * @Template()
 */
public function indexAction() {
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('mainDbBundle:Guest')->findAll();

    return array(
        'entities' => $entities,
    );
}

/**
 * Creates a new Guest entity.
 *
 * @Route("/", name="guest_create")
 * @Method("POST")
 * @Template("mainDbBundle:Guest:new.html.twig")
 */
public function createAction(Request $request) {
    $entity = new Guest();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('guest_show', array('id' => $entity->getId())));
    }

    return array(
        'entity' => $entity,
        'form' => $form->createView(),
    );
}

/**
 * Creates a form to create a Guest entity.
 *
 * @param Guest $entity The entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createCreateForm(Guest $entity) {
    $form = $this->createForm(new GuestType(), $entity, array(
        'action' => $this->generateUrl('guest_create'),
        'method' => 'POST',
    ));

    $form->add('submit', 'submit', array('label' => 'Create'));

    return $form;
}

/**
 * Displays a form to create a new Guest entity.
 *
 * @Route("/new", name="guest_new")
 * @Method("GET")
 * @Template()
 */
public function newAction() {
    $entity = new Guest();
    $form = $this->createCreateForm($entity);

    return array(
        'entity' => $entity,
        'form' => $form->createView(),
    );
}

/**
 * Finds and displays a Guest entity.
 *
 * @Route("/{id}", name="guest_show")
 * @Method("GET")
 * @Template()
 */
public function showAction($id) {
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('mainDbBundle:Guest')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Guest entity.');
    }

    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity' => $entity,
        'delete_form' => $deleteForm->createView(),
    );
}

/**
 * Displays a form to edit an existing Guest entity.
 *
 * @Route("/{id}/edit", name="guest_edit")
 * @Method("GET")
 * @Template()
 */
public function editAction($id) {
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('mainDbBundle:Guest')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Guest entity.');
    }

    $editForm = $this->createEditForm($entity);
    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );
}

/**
 * Creates a form to edit a Guest entity.
 *
 * @param Guest $entity The entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createEditForm(Guest $entity) {
    $form = $this->createForm(new GuestType(), $entity, array(
        'action' => $this->generateUrl('guest_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}

/**
 * Edits an existing Guest entity.
 *
 * @Route("/{id}", name="guest_update")
 * @Method("PUT")
 * @Template("mainDbBundle:Guest:edit.html.twig")
 */
public function updateAction(Request $request, $id) {
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('mainDbBundle:Guest')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Guest entity.');
    }

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createEditForm($entity);
    $editForm->handleRequest($request);

    if ($editForm->isValid()) {
        $em->flush();

        return $this->redirect($this->generateUrl('guest_edit', array('id' => $id)));
    }

    return array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );
}

/**
 * Deletes a Guest entity.
 *
 * @Route("/{id}", name="guest_delete")
 * @Method("DELETE")
 */
public function deleteAction(Request $request, $id) {
    $form = $this->createDeleteForm($id);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('mainDbBundle:Guest')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Guest entity.');
        }

        $em->remove($entity);
        $em->flush();
    }

    return $this->redirect($this->generateUrl('guest'));
}

/**
 * Creates a form to delete a Guest entity by id.
 *
 * @param mixed $id The entity id
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createDeleteForm($id) {
    return $this->createFormBuilder()
                    ->setAction($this->generateUrl('guest_delete', array('id' => $id)))
                    ->setMethod('DELETE')
                    ->add('submit', 'submit', array('label' => 'Delete'))
                    ->getForm()
    ;
}

/**
 *
 * @Route("/", name="guest_search_by_saloon")
 * 
 */
public function getBySaloonAction() {
    return new Response('<html><body>Ciao !</body></html>');
}

}

this is what log says:

NFO - Matched route "guest_show" (parameters: "_controller": "main\dbBundle\Controller\GuestController::showAction", "id": "guest_search_by_saloon", "_route": "guest_show") 
  • 写回答

1条回答 默认 最新

  • duanchao9559 2015-02-19 13:30
    关注

    Ok, my guess is that first of all you're not calling url you're showing us because, as logs says, showAction() is called instead of getBySaloonAction() and this, related to your error, is perfectly sensible.

    If you notice log error, you're passing guest_search_by_saloon (string) as id (integer) somehow and db query is run with $id = 'guest_search_by_saloon' and this lead you to error.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算