0

我的控制器 PurchaseOrder 控制器

<?php

namespace CJ\BusinessBundle\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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use CJ\BusinessBundle\Entity\PurchaseOrder;
use CJ\BusinessBundle\Form\PurchaseOrderType;

/**
 * PurchaseOrder controller.
 *
 * @Route("/purchaseorder")
 */
class PurchaseOrderController extends Controller
{
/**
 * Lists all PurchaseOrder entities.
 *
 * @Route("/", name="purchaseorder")
 * @Method("GET")
 * @Template()
 */
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('CJBusinessBundle:PurchaseOrder')->findAll();

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

/**
 * Creates a new PurchaseOrder entity.
 *
 * @Route("/", name="purchaseorder_create")
 * @Method("POST")
 * @Template("CJBusinessBundle:PurchaseOrder:new.html.twig")
 */
public function createAction(Request $request)
{
    $entity  = new PurchaseOrder();
    $form = $this->createForm(new PurchaseOrderType(), $entity);
    $form->bind($request);

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

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

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

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

    $purchase = $this->get('cj.businessbundle.purchase');
    $purchase->newAction();

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

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

    $entity = $em->getRepository('CJBusinessBundle:PurchaseOrder')->find($id);

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

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

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

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

    $entity = $em->getRepository('CJBusinessBundle:PurchaseOrder')->find($id);

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

    $editForm = $this->createForm(new PurchaseOrderType(), $entity);
    $deleteForm = $this->createDeleteForm($id);

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

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

    $entity = $em->getRepository('CJBusinessBundle:PurchaseOrder')->find($id);

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

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createForm(new PurchaseOrderType(), $entity);
    $editForm->bind($request);

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

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

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

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

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

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

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

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

/**
 * Creates a form to delete a PurchaseOrder entity by id.
 *
 * @param mixed $id The entity id
 *
 * @return Symfony\Component\Form\Form The form
 */
private function createDeleteForm($id)
{
    return $this->createFormBuilder(array('id' => $id))
        ->add('id', 'hidden')
        ->getForm()
    ;
}
}

服务.yml

services:
   cj.businessbundle.purchase:
      class: CJ\BusinessBundle\Controller\PurchaseController

控制器内部的访问方法[不是购买控制器]

$purchase = $this->get('cj.businessbundle.purchase');
$purchase->newAction();

得到错误:

FatalErrorException:错误:在 /home/cj/public_html/Symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php 第 163 行中的非对象上调用成员函数 get()

新的 Action 确实存在

我认为我在定义服务时做错了

4

1 回答 1

6

当您打电话时,$this->get("your.service")您要求依赖注入容器加载该服务。您要求它加载您的控制器之一,该控制器扩展了 Symfony 的控制器类,该控制器类扩展了ContainerAware。您收到的错误是因为加载的控制器可能正在尝试使用$this->container->get("request")但未$this->container设置访问“请求”之类的服务。

ContainerAware 有一个方法setContainer,您可以在设置服务时使用calls:services.yml 中的参数实际运行该方法:

services:
   cj.businessbundle.purchase:
      class: CJ\BusinessBundle\Controller\PurchaseController
      calls:
        - [setContainer, [@service_container]]

Symfony2 为加载的控制器(PurchaseOrderController)执行此操作,但如果您只是自己加载控制器类,则不会。

从 PurchaseController 中提取 newAction(或所需的逻辑)作为服务本身而不是将整个控制器设置为服务会更好。这样,Purchase 和 PurchaseOrder 控制器都可以调用服务来创建新的购买(或任何 newAction 所做的),并且您不会每次都加载整个控制器。查看FOS 用户捆绑用户管理器以获取一个很好的服务示例或http://symfony.com/doc/2.1/book/service_container.html#what-is-a-service

于 2013-04-05T10:21:56.040 回答