zendframework / zend-mvc

Mvc component from Zend Framework
BSD 3-Clause "New" or "Revised" License
105 stars 89 forks source link

Ability to specify response code on controller plugin redirect #313

Open jroedel opened 5 years ago

jroedel commented 5 years ago

I have changed my routing setup on a production app, and I would like to provide search engines with the information that a new URL format is being used. For this I would like to use code 301 (Permanently Moved) instead of 302 (Found).

Code to reproduce the issue

In a controller action:

return $this->redirect()->toUrl('https://github.com'); //, ['statusCode' => 301]);

Expected results

I should be able to specify a statusCode of choosing

Actual results

The browser receives a 302 response code always.

Thanks for all your support!

froschdesign commented 5 years ago

There is an open todo in the class of the redirect plugin:

https://github.com/zendframework/zend-mvc/blob/9f092ff24945f208047d99dff81f67de8862b5f4/src/Controller/Plugin/Redirect.php#L16

In the meantime you can use this workaround in any controller action:

public function indexAction()
{
    $response = $this->getResponse();
    $response->getHeaders()->addHeaderLine(
        'Location',
        'https://github.com'
    );
    $response->setStatusCode(301);

    return $response;
}
weierophinney commented 4 years ago

This repository has been closed and moved to laminas/laminas-mvc; a new issue has been opened at https://github.com/laminas/laminas-mvc/issues/4.