For these situations, I want to auto redirect back to the pages from where I made the request:
- After finishing any action (CRUD etc) - Here I think we need to redirect to 'HTTP_REFERER'
- While surfing or shopping, if login required, so after finishing authentication process, it should redirect back to the same page
A different situation (which is not 'redirect to previous page'):
- Pass a redirect URL (landing page address) in query, for eg: If I send a (external)/URL (ofc encoded) in as GET query parameter (or route part), after login, it should redirect me to this URL
I have already searched over net for this and found some solutions but they are not according to Zend Framework 2. I want all this to be done in zf2 way.
Thanks for any help!
Here is a cool trick.
Create a trait first:
Then an interface:
Then use the interface to inject SessionContainer:
Then in your controller:
This worked for me:
just wanted to share a snippet from my final code that handles if the user comes from another page:
I think referer field is only set when you click on link and let yourself to be redirected to deserved page. Take for example http://www.whatismyreferer.com/. When you google it and click on the link, which takes you to this site, the referer will be the google. But if you copy and paste this url to your web browser, referer page will be undefined. Am i wrong?
Neighter of the above worked for me, but this:
[I would appreciate though, if somebody cleared me up, why the 'referer way' does not work for me! :) Thanks!]
Well, for the CRUD stuff, I'd simply redirect to the routes of the previous action, usually something like
admin-index
,administrate
or whatever. I don't really see why you would need theHTTP_REFERER
in that case. If however you still want to access theHTTP_REFERER
it's as simple as this:For more information see
Zend\Http\Request
A Redirect would be done using the
redirect()
of theZend\Mvc\Controller\AbstractActionController
To see some more live examples of your use-cases, i suggest you check out some of the Modules that pretty much fit your use-cases. Probably the most fitting ones here would be Zf-Commons\ZfcUser and bjyoungblood\BjyAuthorize. For each of those examples i have linked relevant code-samples that may shed some insight to your needs.