@cameron_walter
To create a 301 redirect in Zend Framework 2, you can use the Redirect plugin provided by the Zend MVC framework. Here are the steps to follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
use ZendMvcControllerAbstractActionController; use ZendMvcPluginFlashMessengerFlashMessenger; class YourController extends AbstractActionController { public function yourAction() { // ... $redirectPlugin = $this->plugin('redirect'); // ... } } |
1 2 3 4 5 6 7 8 9 |
public function yourAction() { // ... $redirectPlugin = $this->plugin('redirect'); $redirectPlugin->toUrl('/new-url')->setStatusCode(301); // ... } |
Replace /new-url
with the URL where you want to redirect the user.
1 2 3 4 5 6 7 8 9 |
public function yourAction() { // ... $redirectPlugin = $this->plugin('redirect'); $redirectPlugin->toUrl('/new-url')->setStatusCode(301); return $this->response; } |
Make sure to assign the redirect response to the response
property of your controller.
That's it! You have successfully created a 301 redirect in Zend Framework 2.