Forums › Forums › OroPlatform › OroPlatform – Programming Questions › override one controller from core bundle and keep /api/rest controllers
This topic contains 5 replies, has 2 voices, and was last updated by Mike Kudelya 7 years, 4 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- November 6, 2016 at 11:36 pm #34278
Good day.
I’m trying to override couple of actions from OroEmailBundle/EmailController.
No problems with common controllers through bundle inheritance, I redefined actions that I need and the rest of actions from EmailBundle/Controller/* are fine.
The problem with controllers inside EmailBundle/Controller/Api/Rest/*, they all fall away no matter what I’m trying to write in routing.yml and routing_api.yml.
Could anyone give me any advice or working example of these files to keep rest api controllers alive? - CreatorTopic
- AuthorReplies
- November 9, 2016 at 9:19 am #34279
Hi
Try to use this approach:
Add to src/Custom/Bundle/OroBundle/Resources/config/oro/routing.yml:
YAML123456789custom_oro_bundle_api:resource: "@CustomOroBundle/Resources/config/oro/routing_api.yml"type: restprefix: api/rest/{version}requirements:version: latest|v1_format: jsondefaults:version: latestAdd to src/Custom/Bundle/OroBundle/Resources/config/oro/routing_api.yml:
YAML123oro_email_api:resource: "@CustomOroBundle/Controller/Api/Rest/EmailController.php"type: restand finally api controller src/Custom/Bundle/OroBundle/Controller/Api/Rest/EmailController.php:
PHP123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869<?phpnamespace Custom\Bundle\OroBundle\Controller\Api\Rest;use FOS\RestBundle\Controller\Annotations as Rest;use FOS\RestBundle\Controller\Annotations\QueryParam;use FOS\RestBundle\Controller\Annotations\NamePrefix;use FOS\RestBundle\Controller\Annotations\RouteResource;use Symfony\Component\HttpFoundation\Response;use Nelmio\ApiDocBundle\Annotation\ApiDoc;use Oro\Bundle\SecurityBundle\Annotation\AclAncestor;use Oro\Bundle\EmailBundle\Controller\Api\Rest\EmailController as Controller;use Oro\Bundle\SoapBundle\Request\Parameters\Filter\StringToArrayParameterFilter;/*** @RouteResource("email")* @NamePrefix("oro_api_")*/class EmailController extends Controller{/*** Get emails.** @QueryParam(* name="page",* requirements="\d+",* nullable=true,* description="Page number, starting from 1. Defaults to 1."* )* @QueryParam(* name="limit",* requirements="\d+",* nullable=true,* description="Number of items per page. Defaults to 10."* )* @QueryParam(* name="messageId",* requirements=".+",* nullable=true,* description="The email 'Message-ID' attribute. One or several message ids separated by comma."* )* @ApiDoc(* description="Get emails",* resource=true* )* @AclAncestor("oro_email_email_view")* @return Response*/public function cgetAction(){echo "test message";exit;$page = (int)$this->getRequest()->get('page', 1);$limit = (int)$this->getRequest()->get('limit', self::ITEMS_PER_PAGE);$filterParameters = ['messageId' => new StringToArrayParameterFilter()];$criteria = $this->getFilterCriteria($this->getSupportedQueryParameters(__FUNCTION__),$filterParameters);return $this->handleGetListRequest($page, $limit, $criteria);}}November 9, 2016 at 8:16 pm #34280Hi Mike, thanks for answer, I got all api routes available now.
But new question appeared: is it normal, that some actions (actually all in OroEmailBundle\Controller except EmailController itself, EmailTemplateController as example) don’t have specified names in their @Route annotations? Because there are references from other code (‘oro_email_emailtemplate_index’ as example) which use generated action names, as I understand. So when I override bundle – these actions change their default names and I got ‘route not found’ exceptions. Should I inherit these controllers by myself and specify correct names for routes or it could be fixed in platform code?November 10, 2016 at 8:51 am #34281Hi
Yes, by default @Route(name) is auto-generated. I think it is not normal, that route name is missed, it is historically happened.
The best way to override business logic it is override service class name which this controller called, for example you can do this via Dependency Injection or event listener.
But if you really want to override controller, you should do the following work. Unfortunately EmailTemplateController.php doesn’t contain any route name. You should copy it in your custom bundle and add route name to each action according to original auto-generated name (for example oro_email_emailtemplate_index). If you would have route names in controller, you will override only a few actions. Also you should create your routing.yml:
YAML1234567891011121314151617181920212223242526272829303132333435363738oro_email:resource: "@OroEmailBundle/Controller"type: annotationprefix: /emailoro_email_dashboard:resource: "@OroEmailBundle/Controller/Dashboard"type: annotationprefix: /dashboardoro_email_bundle_api:resource: "@OroEmailBundle/Resources/config/oro/routing_api.yml"type: restprefix: api/rest/{version}requirements:version: latest|v1_format: jsondefaults:version: latestoro_mailbox_config:resource: "@OroEmailBundle/Controller/Configuration"type: annotationprefix: /configcustom_email_email_template:resource: "@OroEmailBundle/Controller/EmailController.php"type: annotationprefix: /emailcustom_email_autoresponse_rule:resource: "@OroEmailBundle/Controller/AutoResponseRuleController.php"type: annotationprefix: /emailcustom_email_autoresponse_origin:resource: "@OroEmailBundle/Controller/OriginController.php"type: annotationprefix: /emailNovember 10, 2016 at 5:33 pm #34282Thank you, Mike. Pretty clear now.
Just in case: will it make any sense if I’ll create a pull request with route names in those two OroEmailBundle controllers?November 11, 2016 at 4:53 am #34283Hi
It will be great!
Thank you in advance!
- AuthorReplies
The forum ‘OroPlatform – Programming Questions’ is closed to new topics and replies.