{"id":2978,"date":"2022-12-08T06:58:40","date_gmt":"2022-12-08T12:58:40","guid":{"rendered":"https:\/\/promincproductions.com\/blog\/?p=2978"},"modified":"2022-12-08T06:58:44","modified_gmt":"2022-12-08T12:58:44","slug":"magento-2-redirect-to-404-from-controller","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/magento-2-redirect-to-404-from-controller\/","title":{"rendered":"Magento 2 Redirect to 404 from Controller"},"content":{"rendered":"<p>There are many times a Magento 2 controller should be forwarded to a 404 (<code>noroute<\/code>) path &#8211; making a <code>GET<\/code> request for a path that should only respond to a <code>POST<\/code> request, a dynamic controller that allows some but not all paths, etc.  This sounds simple, right?  It is, but for some reason I always forget the proper syntax and spend way too long looking it up&#8230;  Hopefully this post can save you some time like I feel it will for me.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"600\" height=\"600\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2022\/12\/magento-404-redirect-how-to.png\" alt=\"How to forward a request to a 404 page from a Magento 2 controller\" class=\"wp-image-2982\" title=\"How to forward a request to a 404 page from a Magento 2 controller\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2022\/12\/magento-404-redirect-how-to.png 600w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2022\/12\/magento-404-redirect-how-to-500x500.png 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2022\/12\/magento-404-redirect-how-to-150x150.png 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2022\/12\/magento-404-redirect-how-to-450x450.png 450w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<p>The Magento 2 router can be used to forward any request to another action\/controller as needed.  In the case of this tutorial, the request needs to be forwarded to what Magento calls the <code>noroute<\/code> action.  The <code>noroute<\/code> action will set the header <code>Status Code<\/code> to <code>404<\/code> (<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Status\/404\" target=\"_blank\" rel=\"noopener\" data-lasso-id=\"836\">not found<\/a>).   The configured Magento 404 page will be returned to the visitor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use Magento <code>ResultFactory<\/code> to Redirect to <code>noroute<\/code><\/h2>\n\n\n\n<p>The Magento framework class of <code>Magento\\Framework\\Controller\\ResultFactory<\/code> [<a href=\"https:\/\/github.com\/thejinxters\/magento2-testing\/blob\/master\/lib\/internal\/Magento\/Framework\/Controller\/ResultFactory.php\" target=\"_blank\" rel=\"noopener\" data-lasso-id=\"837\">source<\/a>] is used to forward to the <code>noroute<\/code> action.  This handles setting the proper headers and obtaining the proper <code>404<\/code> page content from Magento.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/** @var Forward $resultForward *\/\n$resultForward = $this-&gt;resultFactory-&gt;create(ResultFactory::TYPE_FORWARD);\n$resultForward-&gt;forward('noroute');\nreturn $resultForward;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Controller Example<\/h2>\n\n\n\n<p>Here&#8217;s a very simplified example controller that checks if the request has <code>POST<\/code> data.  If it does, process the controller logic.  If not, return a <code>404<\/code> (or <code>noroute<\/code> as Magento calls it) response.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># file: app\/code\/Vendor\/Namespace\/Controller\/ActionName.php\n\n&lt;?php\n\nnamespace Vendor\\Namespace\\Controller;\n\nuse Magento\\Framework\\Controller\\ResultFactory;\n\nclass ActionName\n{\n\n    \/**\n     * @var resultFactory\n     *\/\n    protected $resultFactory;\n\n    \/**\n     * @param ResultFactory $resultFactory\n     *\/\n    public function __construct(\n        ResultFactory $resultFactory\n    ) {\n        $this-&gt;resultFactory = $resultFactory;\n    }\n\n    public function execute()\n    {\n        $postData = $this-&gt;getRequest()-&gt;getPostValue();\n\n        if( empty( $postData ) ) {\n            \/\/ no post data, thus return a 404\n            \/** @var Forward $resultForward *\/\n            $resultForward = $this-&gt;resultFactory-&gt;create(ResultFactory::TYPE_FORWARD);\n            $resultForward-&gt;forward('noroute');\n            return $resultForward;\n        } else {\n            \/\/ valid request, process controller logic\n        }\n    }\n\n}<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>There are many times a Magento 2 controller should be forwarded to a 404 (noroute) path &#8211; making [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2982,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[46],"tags":[345],"class_list":["post-2978","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento","tag-magento-2"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2022\/12\/magento-404-redirect-how-to.png","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-M2","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/2978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=2978"}],"version-history":[{"count":5,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/2978\/revisions"}],"predecessor-version":[{"id":2987,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/2978\/revisions\/2987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/2982"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=2978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=2978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=2978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}