Magento 2 Redirect to admin shipment view page

Generally, as Magento developers, we copy the redirect path from the browser URL, but it does not work for many admin URLs.
When you want to redirect to the shipment view page in the Magento 2 admin page, for that you copy the shipment controller path from the browser and set to redirect path but it’s redirecting to 404 even though the same path you used which shows in the browser of shipment view page. At this stage you might get stuck because all looks good then why it’s not redirecting to the shipment page in Magento 2 admin? Don’t worry we Dhairvi Solutions explain you what you should do and what you are doing wrong.

Path for admin shipment view page in Magento 2

The correct path of the admin shipment view page is “adminhtml/order_shipment/view”. So the code for redirecting to the shipment view page in Magenot 2 is as below.

$resultRedirect->setPath(
    'adminhtml/order_shipment/view',
    ['shipment_id' => $shipmentId]
);

Most of the time developers try to access the admin shipment view page using “admin/order_shipment/view” because this URL is what you are seeing in the browser URL of the shipment page, but this is the wrong one because the ID of the admin route is “adminhtml” and you must use route ID, not frontName for redirection.

How admin routes work in Magento 2

Below is the adminhtml routes.xml which shows why you have to use “adminhtml” not “admin” for redirection. Generally, you see the route ID and frontName is the same but for native Magento admin routes.xml it’s a different one. So when you want to use it in code then you have to use ID and when you want to use this as a browser URL then you have to use frontName.

magento adminhtml routes.xml

I hope the new developer learns something new from his blog post by Dhairvi Solutions 🙂