How to use redirect() outside controller in zend 2

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use redirect() outside controller in zend 2

Arvind Jha
$renderer = $sm->get('Zend\View\Renderer\RendererInterface');
$url = $renderer->basePath('/album/index/login');
return $this->redirect()->toUrl($url);

Above code work in controller but not in module.php
Reply | Threaded
Open this post in threaded view
|

Re: How to use redirect() outside controller in zend 2

Marco Pivetta
Why would you want to use the redirect helper in a module?

Anyway, the redirect helper code is trivial: https://github.com/zendframework/zf2/blob/8615f24ebdfa905cc719be3c57649e93ea40ef20/library/Zend/Mvc/Controller/Plugin/Redirect.php#L62-L65


On 16 January 2015 at 11:35, Arvind Jha [via Zend Framework Community] <[hidden email]> wrote:
$renderer = $sm->get('Zend\View\Renderer\RendererInterface');
$url = $renderer->basePath('/album/index/login');
return $this->redirect()->toUrl($url);

Above code work in controller but not in module.php


If you reply to this email, your message will be added to the discussion below:
http://zend-framework-community.634137.n4.nabble.com/How-to-use-redirect-outside-controller-in-zend-2-tp4662425.html
To start a new topic under Zend Framework, email [hidden email]
To unsubscribe from Zend Framework Community, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: How to use redirect() outside controller in zend 2

Arvind Jha
Thanks for the respose

Actually, I'm working on an ACL which is called in Module.php and attached to the bootstrap.

Obviously the ACL restricts access to certain areas of the site, which brings the need for redirects.
Reply | Threaded
Open this post in threaded view
|

Re: How to use redirect() outside controller in zend 2

Gina-Marie Rollock
This works for me in Module.php:

$target         = $e->getTarget();
return $target->redirect()->toUrl('/auth/login');

where $e is the event.



Gina-Marie Rollock
Software Developer
Network Technology Solutions
Office  119 East Jackson Street, Thomasville, GA 31792
Phone 229.584.2006   Fax 229.226.2495
Email  [hidden email]
Web  http://www.ntsnetworks.com


-----Original Message-----
From: Arvind Jha [mailto:[hidden email]]
Sent: Friday, January 16, 2015 8:40 AM
To: [hidden email]
Subject: [fw-general] Re: How to use redirect() outside controller in zend 2

Thanks for the respose

Actually, I'm working on an ACL which is called in Module.php and attached to the bootstrap.

Obviously the ACL restricts access to certain areas of the site, which brings the need for redirects.



--
View this message in context: http://zend-framework-community.634137.n4.nabble.com/How-to-use-redirect-outside-controller-in-zend-2-tp4662425p4662427.html
Sent from the Zend Framework mailing list archive at Nabble.com.

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]






--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: How to use redirect() outside controller in zend 2

Arvind Jha
Thanks Gina

I tried but it says
Fatal error: Call to undefined method Zend\Mvc\Application::redirect()

Any idea why?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: How to use redirect() outside controller in zend 2

Alejandro Celaya
Why don't you just return a response object with a location header and a
30x status code.

--
Alejandro Celaya Alastrué
www.alejandrocelaya.com
El 16/1/2015 15:11, "Arvind Jha" <[hidden email]> escribió:

> Thanks Gina
>
> I tried but it says
> Fatal error: Call to undefined method Zend\Mvc\Application::redirect()
>
> Any idea why?
>
> Thanks
>
>
>
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/How-to-use-redirect-outside-controller-in-zend-2-tp4662425p4662429.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: How to use redirect() outside controller in zend 2

Gina-Marie Rollock
In reply to this post by Arvind Jha
I took out the rest of my code, but here's the basics. This is how I call my function from onBootstrap and it works fine for me.

If this doesn't work for you, not sure if I could provide more assistance. :) Maybe because I have it attached to the "dispatch" event? Not sure what event you have your function attached to.

public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();

     $moduleRouteListener->attach($eventManager);

     $eventManager->attach('dispatch', array($this, 'secureSession'));
}

public function secureSession($e)
{
    $target         = $e->getTarget();
    return $target->redirect()->toUrl('/auth/login');
}



Gina-Marie Rollock
Software Developer
Network Technology Solutions
Office  119 East Jackson Street, Thomasville, GA 31792
Phone 229.584.2006   Fax 229.226.2495
Email  [hidden email]
Web  http://www.ntsnetworks.com


-----Original Message-----
From: Arvind Jha [mailto:[hidden email]]
Sent: Friday, January 16, 2015 9:13 AM
To: [hidden email]
Subject: [fw-general] Re: How to use redirect() outside controller in zend 2

Thanks Gina

I tried but it says
Fatal error: Call to undefined method Zend\Mvc\Application::redirect()

Any idea why?

Thanks




--
View this message in context: http://zend-framework-community.634137.n4.nabble.com/How-to-use-redirect-outside-controller-in-zend-2-tp4662425p4662429.html
Sent from the Zend Framework mailing list archive at Nabble.com.

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]






--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: How to use redirect() outside controller in zend 2

Arvind Jha
Thanks again

This worked for me. :)

Arvind Jha
Reply | Threaded
Open this post in threaded view
|

Re: How to use redirect() outside controller in zend 2

samsonasik
This post has NOT been accepted by the mailing list yet.
In reply to this post by Gina-Marie Rollock
you should can do with response object :
     
     $response = $e->getResponse();
               
     $response->setStatusCode(302);
     $response->getHeaders()->addHeaderLine('Location', '/auth/login');
     
     $e->stopPropagation();


or, do forward instead :

    $e->getRouteMatch()
                    ->setParam('controller', 'Auth\Controller\Auth')
                    ->setParam('action', 'login');