Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to specify response code on controller plugin redirect #4

Closed
weierophinney opened this issue Dec 31, 2019 · 4 comments
Closed
Labels
Question Further information is requested

Comments

@weierophinney
Copy link
Member

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!


Originally posted by @jroedel at zendframework/zend-mvc#313

@weierophinney
Copy link
Member Author

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;
}

Originally posted by @froschdesign at zendframework/zend-mvc#313 (comment)

samsonasik added a commit to samsonasik/laminas-mvc that referenced this issue Aug 7, 2020
samsonasik added a commit to samsonasik/laminas-mvc that referenced this issue Aug 7, 2020
samsonasik added a commit to samsonasik/laminas-mvc that referenced this issue Aug 7, 2020
@samsonasik
Copy link
Member

I created PR #61 for it

@chaos0815
Copy link
Contributor

Would it be a solution to pass the desired status code as an optional parameter to the toUrl() method and as an $option = ['statusCode' => 301] to the toRoute() method?

@Ocramius
Copy link
Member

Ocramius commented Dec 5, 2022

As mentioned on slack ( https://laminas.slack.com/archives/C4R13GTRD/p1670227522366209 )

return $this
    ->redirect()
    ->toUrl('https://github.com')
    ->setStatusCode(301);

@Ocramius Ocramius closed this as completed Dec 5, 2022
@Ocramius Ocramius added the Question Further information is requested label Dec 5, 2022
Ocramius added a commit that referenced this issue Dec 5, 2022
improves Redirect plugin docs showing how to set status code. #4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants