# Redirect from Plugin

**URL:** <https://discourse.laminas.dev/t/redirect-from-plugin/2319>\
**Category:** Components & MVC\
**Created:** [May 26, 2021, 9:37am UTC](https://discourse.laminas.dev/t/redirect-from-plugin/2319 "2021-05-26T09:37:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kriss\_Kals](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kriss_kals/32/1295_2.png) [@Kriss\_Kals](https://discourse.laminas.dev/u/Kriss_Kals)\
**Post date:** [May 26, 2021, 9:37am UTC](https://discourse.laminas.dev/t/redirect-from-plugin/2319/1 "2021-05-26T09:37:03Z")

</div>

Hello,

could someone please drop an example of how to make a redirect from a plugin?

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [May 26, 2021, 9:50am UTC](https://discourse.laminas.dev/t/redirect-from-plugin/2319/2 "2021-05-26T09:50:16Z")

</div>

> [@Kriss\_Kals](#):
>
> …how to make a redirect from a plugin?

What plugin do you mean? Controller plugin?

Please also describe your use case because it seems strange to want to do it that way.

---

<div class="post-metadata">

**Author:** ![Kriss\_Kals](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kriss_kals/32/1295_2.png) [@Kriss\_Kals](https://discourse.laminas.dev/u/Kriss_Kals)\
**Post date:** [May 26, 2021, 11:05am UTC](https://discourse.laminas.dev/t/redirect-from-plugin/2319/3 "2021-05-26T11:05:15Z")

</div>

I have an ExampleHelper which extends AbstractHelper and in an exceptional situation I would like to redirect the user to the route e.g. logout (in method\_\_invoke). Is it possible?

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [May 26, 2021, 1:12pm UTC](https://discourse.laminas.dev/t/redirect-from-plugin/2319/4 "2021-05-26T13:12:22Z")

</div>

> [@Kriss\_Kals](#):
>
> I have an ExampleHelper which extends AbstractHelper…

I assume you mean `Laminas\View\Helper\AbstractHelper`.

> [@Kriss\_Kals](#):
>
> Is it possible?

Maybe but definitely wrong. A redirect in a view helper is too late, this must be done earlier.  
Can you provide the code of your view helper?

---

<div class="post-metadata">

**Author:** ![Kriss\_Kals](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kriss_kals/32/1295_2.png) [@Kriss\_Kals](https://discourse.laminas.dev/u/Kriss_Kals)\
**Post date:** [May 26, 2021, 6:58pm UTC](https://discourse.laminas.dev/t/redirect-from-plugin/2319/5 "2021-05-26T18:58:09Z")

</div>

You are right, it is indeed too late. I now have the logic in a plugin (ExamplePlugin extends AbstractPlugin) and the current question is, in the method \_\_invoke can I do the redirect action?

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [May 26, 2021, 7:17pm UTC](https://discourse.laminas.dev/t/redirect-from-plugin/2319/6 "2021-05-26T19:17:43Z")

</div>

> [@Kriss\_Kals](#):
>
> and the current question is, in the method \_\_invoke can I do the redirect action?

In a controller plugin, you have access to the controller and thus also to [all other plugins](https://docs.laminas.dev/laminas-mvc/plugins/#redirect-plugin).

_Unfortunately, I cannot say if this is a correct implementation, because your use case is still a mystery._

---

<div class="post-metadata">

**Author:** ![Kriss\_Kals](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kriss_kals/32/1295_2.png) [@Kriss\_Kals](https://discourse.laminas.dev/u/Kriss_Kals)\
**Post date:** [May 26, 2021, 7:47pm UTC](https://discourse.laminas.dev/t/redirect-from-plugin/2319/7 "2021-05-26T19:47:43Z")

</div>

In the plugin, in the \_\_invoke method, I have fetched the user’s data from the session - it returns all the necessary information about the user, but I would like to do a redirect to route logout if the session does not return the user’s data (e.g. it happens when the user has a session, but his record has been deleted from the database during his session). For example in my UserPlugin (I use this plugin in many controllers, in general this solution works, but there may be a specific situation described above):

```auto
...
if (! $this->authorizationService instanceof AuthorizationService) {
			throw new \Exception(
				'No AuthorizationService instance provided.'
			);
		}

		if (! $this->authorizationService->getIdentity()){
			return;
		}
		
		$userRow = $this->usersTable->fetchAccountById((int) $this->authorizationService->getIdentity()->getUserId());

		if (! $userRow) {
			// do redirect to logout
		} else {
			return $userRow;
		}
...

```

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [May 26, 2021, 8:22pm UTC](https://discourse.laminas.dev/t/redirect-from-plugin/2319/8 "2021-05-26T20:22:40Z")

</div>

> In a controller plugin, you have access to the controller…

```php
if (! $userRow) {
    return $this->getController()->redirect()->toRoute('logout');
    // or
    return $this->getController()->plugin('redirect')->toRoute('logout');
} else {
    return $userRow;
}

```

Normally the identity comes from the authentication service, not authorization – these are two different things. See the related controller plugin: [laminas-mvc-plugin-identity - Laminas Docs](https://docs.laminas.dev/laminas-mvc-plugin-identity/)

> [@Kriss\_Kals](#):
>
> …but I would like to do a redirect to route logout if the session does not return the user’s data…

I would suggest to use the standard authentication service from laminas-authentication and the standard identity controller plugin.  
The check if the session data or user data is invalid can be done listener which listen on a MVC event. The listener can be executed automatically and always. Maybe you already have an ACL guard or something similar which does this job.

---

<div class="post-metadata">

**Author:** ![Kriss\_Kals](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kriss_kals/32/1295_2.png) [@Kriss\_Kals](https://discourse.laminas.dev/u/Kriss_Kals)\
**Post date:** [May 26, 2021, 9:28pm UTC](https://discourse.laminas.dev/t/redirect-from-plugin/2319/9 "2021-05-26T21:28:10Z")

</div>

I use LmcRbacMvc\Service\AuthorizationService and I think everything is working ok.

I only have a problem with a user who was deleted during his session, and in some controllers I use this plugin to retrieve his data from the database, and he doesn’t exist anymore (he was deleted when he logged in, or in the session he still has his data, but it’s really not in the database anymore).

> The check if the session data or user data is invalid can be done listener which listen on a MVC event. The listener can be executed automatically and always.

This is my solution:

```auto
public function onBootstrap($e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $eventManager->attach(MvcEvent::EVENT_DISPATCH, [$this, 'checkUser']);
    }

    public function checkUser(MvcEvent $e)
    {
        $serviceManager = $e->getApplication()->getServiceManager();
        $usersTable = $serviceManager->get(UsersTable::class);

        $authorizationService = $serviceManager->get(AuthorizationService::class);
        if ($authorizationService->getIdentity()) {
            $userRow = $usersTable->fetchAccountById((int) $authorizationService->getIdentity()->getUserId());
            
            if (! $userRow) {
                $response = $e->getResponse();
                $response->getHeaders()->addHeaderLine('Location', '/logout');
                $response->setStatusCode(302);
                $response->sendHeaders();
                return $response;   
            } else {
                return $userRow;
            }
        }
        return;
    }

```

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [May 27, 2021, 6:34am UTC](https://discourse.laminas.dev/t/redirect-from-plugin/2319/10 "2021-05-27T06:34:40Z")

</div>

> [@Kriss\_Kals](#):
>
> I use LmcRbacMvc\Service\AuthorizationService and I think everything is working ok.

Now it becomes clear.

Also there it is an authentication service:

> <https://github.com/LM-Commons/LmcRbacMvc/blob/f06a7e289fd559e92b7cbff9b532112fee1b0baf/src/Identity/AuthenticationIdentityProvider.php#L49-L52>

But the identity provider is injected in role service and authorization service. Therefore my confusion.

* * *

Please have a look at the [laminas-session validators](https://docs.laminas.dev/laminas-session/validator/), maybe this is a better solution to check if the session data is still valid. You can write a custom validator.
