{"id":195836,"date":"2019-09-04T13:44:33","date_gmt":"2019-09-04T13:44:33","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=195836"},"modified":"2024-06-24T09:44:55","modified_gmt":"2024-06-24T09:44:55","slug":"creating-post-method-controller-in-magento-2-3","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/","title":{"rendered":"Creating POST Method Controller in Magento 2.3"},"content":{"rendered":"\n<p>In Magento 2.3, a POST method controller can be created by implementing <strong>Magento\\Framework\\App\\Action\\HttpPostActionInterface<\/strong>.<\/p>\n\n\n\n<p> But If we have lots of POST controllers in our Module, then there will need to implement this Interface in all Controllers.<\/p>\n\n\n\n<p>So, In this article, we will explain to you an easy and efficient way to create POST controllers in Magento 2.3.<\/p>\n\n\n\n<p>Firstly, create an ApiController Class, which is implementing <strong>\\Magento\\Framework\\App\\CsrfAwareActionInterface <\/strong>and in this class<strong>,<\/strong> we have implemented two methods named as <strong>createCsrfValidationException<\/strong> and <strong>validateForCsrf<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">namespace Vendor\\Module\\Controller;\n\nuse Magento\\Framework\\Controller\\ResultFactory;\nuse Magento\\Framework\\App\\RequestInterface;\nuse Magento\\Framework\\App\\Request\\InvalidRequestException;\n\nabstract class ApiController extends \\Magento\\Framework\\App\\Action\\Action implements \\Magento\\Framework\\App\\CsrfAwareActionInterface\n{\n     <code>protected $_helper;<\/code>\n<code> <\/code>\n     <code>public function __construct(\\Magento\\Framework\\App\\Action\\Context $context ) {<\/code>\n<code>         parent::__construct($context); <\/code>\n     <code>} <\/code>\n    <code>\/** * @inheritDoc *\/ <\/code>\n    <code>public function createCsrfValidationException( RequestInterface $request ): ?       InvalidRequestException { <\/code>\n<code>         return null; <\/code>\n    <code>} <\/code>\n     <code>\/** * @inheritDoc *\/ <\/code>\n    <code>public function validateForCsrf(RequestInterface $request): ?bool {     <\/code>\n        <code>return true; <\/code>\n   <code>}<\/code>\n}<\/pre>\n\n\n\n<p>Then, we extend the ApiController class in our POST\/GET method controllers.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">namespace Vendor\\Module\\Controller\\Contact;\nclass Post extends \\Vendor\\Module\\Controller\\ApiController {\n  <code>public function execute() {    <\/code>\n     <code>\/\/ write your code here <\/code>\n  <code>}<\/code>\n}<\/pre>\n\n\n\n<p>I hope It will help you. Thanks \ud83d\ude42 \ud83d\ude42<br><br><strong>Previous Blog: <\/strong><a href=\"https:\/\/webkul.com\/blog\/profiler-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Profiler in Magento 2<\/a><br><br><strong>Next Blog: <\/strong><a href=\"https:\/\/webkul.com\/blog\/display-collection-records-in-a-specific-order-except-for-desc-or-asc-sort-order\/\" target=\"_blank\" rel=\"noreferrer noopener\">Display collection records in a specific order except for DESC or ASC sort order<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Magento 2.3, a POST method controller can be created by implementing Magento\\Framework\\App\\Action\\HttpPostActionInterface. But If we have lots of POST controllers in our Module, then there will need to implement this Interface in all Controllers. So, In this article, we will explain to you an easy and efficient way to create POST controllers in Magento <a href=\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":249,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121,1864],"tags":[2713,357,7629,1147],"class_list":["post-195836","post","type-post","status-publish","format-standard","hentry","category-magento-2","category-mobikul","tag-controller","tag-csrf","tag-magento2-3","tag-post"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating POST Method Controller in Magento 2.3 - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating POST Method Controller in Magento 2.3 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"In Magento 2.3, a POST method controller can be created by implementing MagentoFrameworkAppActionHttpPostActionInterface. But If we have lots of POST controllers in our Module, then there will need to implement this Interface in all Controllers. So, In this article, we will explain to you an easy and efficient way to create POST controllers in Magento [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-04T13:44:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-24T09:44:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Khushboo Sahu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khushboo Sahu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/\"},\"author\":{\"name\":\"Khushboo Sahu\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\"},\"headline\":\"Creating POST Method Controller in Magento 2.3\",\"datePublished\":\"2019-09-04T13:44:33+00:00\",\"dateModified\":\"2024-06-24T09:44:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/\"},\"wordCount\":130,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"controller\",\"csrf\",\"magento2.3\",\"post\"],\"articleSection\":[\"Magento 2\",\"Mobikul\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/\",\"url\":\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/\",\"name\":\"Creating POST Method Controller in Magento 2.3 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2019-09-04T13:44:33+00:00\",\"dateModified\":\"2024-06-24T09:44:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating POST Method Controller in Magento 2.3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\",\"name\":\"Khushboo Sahu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Khushboo Sahu\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating POST Method Controller in Magento 2.3 - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/","og_locale":"en_US","og_type":"article","og_title":"Creating POST Method Controller in Magento 2.3 - Webkul Blog","og_description":"In Magento 2.3, a POST method controller can be created by implementing MagentoFrameworkAppActionHttpPostActionInterface. But If we have lots of POST controllers in our Module, then there will need to implement this Interface in all Controllers. So, In this article, we will explain to you an easy and efficient way to create POST controllers in Magento [...]","og_url":"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2019-09-04T13:44:33+00:00","article_modified_time":"2024-06-24T09:44:55+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Khushboo Sahu","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Khushboo Sahu","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/"},"author":{"name":"Khushboo Sahu","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca"},"headline":"Creating POST Method Controller in Magento 2.3","datePublished":"2019-09-04T13:44:33+00:00","dateModified":"2024-06-24T09:44:55+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/"},"wordCount":130,"commentCount":2,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["controller","csrf","magento2.3","post"],"articleSection":["Magento 2","Mobikul"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/","url":"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/","name":"Creating POST Method Controller in Magento 2.3 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2019-09-04T13:44:33+00:00","dateModified":"2024-06-24T09:44:55+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/creating-post-method-controller-in-magento-2-3\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Creating POST Method Controller in Magento 2.3"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca","name":"Khushboo Sahu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Khushboo Sahu"},"url":"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/195836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/249"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=195836"}],"version-history":[{"count":7,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/195836\/revisions"}],"predecessor-version":[{"id":448167,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/195836\/revisions\/448167"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=195836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=195836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=195836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}