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!
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 the HTTP_REFERER
in that case. If however you still want to access the HTTP_REFERER
it's as simple as this:
//SomeController.php
$redirectUrl = $this->getRequest()->getHeader('HTTP_REFERER', $defaultValue);
For more information see Zend\Http\Request
A Redirect would be done using the redirect()
of the Zend\Mvc\Controller\AbstractActionController
$this->redirect()->toUrl($redirectUrl); //or using a route:
$this->redirect()->toRoute('some/route');
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.
This worked for me:
$url = $this->getRequest()->getHeader('Referer')->getUri();
$this->redirect()->toUrl($url);
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?
Here is a cool trick.
Create a trait first:
<?php
namespace Application\Controller\Plugin;
use Zend\Session;
trait RefererRedirect
{
/**
* @var Session\Container
*/
private $sessionContainer;
/**
* @param Session\Container $sessionContainer
*/
public function setSessionContainer(Session\Container $sessionContainer)
{
$this->sessionContainer = $sessionContainer;
}
protected function clearReferer()
{
$this->sessionContainer->offsetUnset('referer');
}
protected function registerReferer()
{
if (!$this->sessionContainer->offsetExists('referer')) {
$this->sessionContainer->offsetSet('referer', $this->getRequest()->getHeader('Referer')->uri()->getPath());
}
}
protected function redirectToRefererOrDefaultRoute($defaultRoute, $defaultParams = [])
{
if ($this->sessionContainer->offsetExists('referer')) {
$url = $this->sessionContainer->offsetGet('referer');
$this->clearReferer();
return $this->redirect()->toUrl($url);
}
return $this->redirect()->toRoute($defaultRoute, $defaultParams);
}
}
Then an interface:
<?php
namespace Application;
use Zend\Session;
interface RefererAwareInterface
{
/**
* @param Session\Container $sessionContainer
*/
public function setSessionContainer(Session\Container $sessionContainer);
}
Then use the interface to inject SessionContainer:
<?php
namespace Application;
class Module
{
public function getControllerConfig()
{
return [
'factories' => [
],
'initializers' => array(
function ($instance, $sm) {
if ($instance instanceof RefererAwareInterface) {
$instance->setSessionContainer(new Session\Container('Referer'));
}
}
)
];
}
}
Then in your controller:
<?php
namespace Auth\Controller;
class UsersController extends AbstractActionController implements RefererAwareInterface
{
use RefererRedirect;
public function indexAction()
{
$this->clearReferer();
// do stuff
}
public function addAction()
{
$this->registerReferer();
// do stuff
return $this->redirectToRefererOrDefaultRoute('auth/users');
}
public function backAction()
{
return $this->redirectToRefererOrDefaultRoute('auth/users');
}
}
Neighter of the above worked for me, but this:
$url = $this->getRequest()->getHeaders('cookie')->original_url;
$this->redirect()->toUrl($url);
[I would appreciate though, if somebody cleared me up, why the 'referer way' does not work for me! :) Thanks!]
just wanted to share a snippet from my final code that handles if the user comes from another page:
$referer = $this->getRequest()->getHeader('Referer');
if ($referer) {
$refererUrl = $referer->uri()->getPath(); // referer url
$refererHost = $referer->uri()->getHost(); // referer host
$host = $this->getRequest()->getUri()->getHost(); // current host
// only redirect to previous page if request comes from same host
if ($refererUrl && ($refererHost == $host)) {
return $this->redirect()->toUrl($refererUrl);
}
}
// redirect to home if no referer or from another page
return $this->redirect()->toRoute('home');