php - Accessing Method Through Service --- Symfony2 -
my controller purchaseorder controller
<?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 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 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 form create 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 , displays 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 find purchaseorder entity.'); } $deleteform = $this->createdeleteform($id); return array( 'entity' => $entity, 'delete_form' => $deleteform->createview(), ); } /** * displays form edit 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 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 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 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 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 find purchaseorder entity.'); } $em->remove($entity); $em->flush(); } return $this->redirect($this->generateurl('purchaseorder')); } /** * creates form delete purchaseorder entity id. * * @param mixed $id entity id * * @return symfony\component\form\form form */ private function createdeleteform($id) { return $this->createformbuilder(array('id' => $id)) ->add('id', 'hidden') ->getform() ; } }
serivces.yml
services: cj.businessbundle.purchase: class: cj\businessbundle\controller\purchasecontroller
accessing method inside controller [not purchase controller]
$purchase = $this->get('cj.businessbundle.purchase'); $purchase->newaction();
getting error:
fatalerrorexception: error: call member function get() on non-object in /home/cj/public_html/symfony/vendor/symfony/symfony/src/symfony/bundle/frameworkbundle/controller/controller.php line 163
the new action exist
i think doing wrong while defining service
when call $this->get("your.service")
asking dependency injection container load service. requesting loads 1 of controllers extends symfony's controller class extends containeraware. error getting because loaded controller trying access service "request" using $this->container->get("request")
$this->container
not set.
containeraware has method setcontainer
can run when service setup using calls:
argument in services.yml:
services: cj.businessbundle.purchase: class: cj\businessbundle\controller\purchasecontroller calls: - [setcontainer, [@service_container]]
symfony2 loaded controller (purchaseordercontroller) not if load controller class yourself.
it better practice extract newaction (or required logic) purchasecontroller service rather setting whole controller service. way both purchase , purchaseorder controllers can call service create new purchase (or whatever newaction does) , not loading whole controller everytime. check out fos user bundle user manager example of service or http://symfony.com/doc/2.1/book/service_container.html#what-is-a-service
Comments
Post a Comment