# How to redirect to home page from a subscriber file?

**URL:** https://forum.shopware.com/t/how-to-redirect-to-home-page-from-a-subscriber-file/65104
**Category:** Programming (EN)
**Created:** [2. März 2020 um 07:02 UTC](https://forum.shopware.com/t/how-to-redirect-to-home-page-from-a-subscriber-file/65104 "2020-03-02T07:02:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![abinjohnedamana](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/abinjohnedamana/32/17378_2.png) [@abinjohnedamana](https://forum.shopware.com/u/abinjohnedamana)
#### Post date: [2. März 2020 um 07:02 UTC](https://forum.shopware.com/t/how-to-redirect-to-home-page-from-a-subscriber-file/65104/1 "2020-03-02T07:02:06Z")

</div>

Hi

&nbsp;

I am using the&nbsp;ProductEvents:: PRODUCT\_LOADED\_EVENT subscriber. I want to redirect to the home page in certain conditions.&nbsp;

&nbsp; &nbsp; public static function getSubscribedEvents(): array  
&nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; return [  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProductEvents::PRODUCT\_LOADED\_EVENT =\> ‘onProductLoaded’  
&nbsp; &nbsp; &nbsp; &nbsp; ];  
&nbsp; &nbsp; }

&nbsp; &nbsp; public function onProductLoaded(EntityLoadedEvent $event)  
&nbsp; &nbsp; {

&nbsp; &nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; //Some conditions  
&nbsp;&nbsp; &nbsp;if()  
&nbsp;&nbsp; &nbsp;{  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$pageVisible = false;  
&nbsp;&nbsp; &nbsp;}

&nbsp; &nbsp; &nbsp; &nbsp; if(!$pageVisible)  
&nbsp; &nbsp; &nbsp; &nbsp; {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // redirect to homepage  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this-\>redirectToRoute(‘frontend.home.page’);

&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; return;  
&nbsp; &nbsp; }

How to redirect to the home page from a subscriber file?

---

<div class="post-metadata">

### Author: ![abinjohnedamana](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/abinjohnedamana/32/17378_2.png) [@abinjohnedamana](https://forum.shopware.com/u/abinjohnedamana)
#### Post date: [9. März 2020 um 04:36 UTC](https://forum.shopware.com/t/how-to-redirect-to-home-page-from-a-subscriber-file/65104/2 "2020-03-09T04:36:46Z")

</div>

If you want to redirect in Shopware way you have to use an event like “RequestEvent”.

```
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Symfony\Component\HttpKernel\Event\RequestEvent;
    use Symfony\Component\HttpKernel\KernelEvents;

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::REQUEST => 'onCmsPageLoaded'
        ];
    }

    public function onCmsPageLoaded(RequestEvent $event)
    {
        $request = $event->getRequest();

        $attributes = $request->attributes;

        $route = $attributes->get('_route');

        if($route == 'frontend.cms.page')
        {
	    $response = new RedirectResponse($this->router->generate('frontend.home.page'), 301);

	    $event->setResponse($response);

	    return;
        }
    }

```

&nbsp;

---

<div class="post-metadata">

### Author: ![lucamario](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/lucamario/32/15269_2.png) [@lucamario](https://forum.shopware.com/u/lucamario)
#### Post date: [16. Dezember 2021 um 17:40 UTC](https://forum.shopware.com/t/how-to-redirect-to-home-page-from-a-subscriber-file/65104/3 "2021-12-16T17:40:56Z")

</div>

Hi @abinjohnedamana,

how do you access event specific data in your case?

I need information about a logged in customer from the context but with the Kernel Request Event I don’t have access to the context anymore.

Vice versa I am not able to redirect if I use a non-kernel event

---

<div class="post-metadata">

### Author: ![abinjohnedamana](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/abinjohnedamana/32/17378_2.png) [@abinjohnedamana](https://forum.shopware.com/u/abinjohnedamana)
#### Post date: [17. Dezember 2021 um 04:17 UTC](https://forum.shopware.com/t/how-to-redirect-to-home-page-from-a-subscriber-file/65104/4 "2021-12-17T04:17:34Z")

</div>

Hi @lucamario

You can try the following

```auto
KernelEvents::RESPONSE => 'onProductDetailPageLoaded',

public function onProductDetailPageLoaded(ResponseEvent $event): void
{
	if ($event->getResponse() instanceof StorefrontResponse) {
	    $this->checkCadcamOrScannerProduct($event);
	    return;
	}
}

public function checkCadcamOrScannerProduct($event)
{
	$route = $event->getRequest()->attributes->get('_route');

    /** @var StorefrontResponse */
    $response = $event->getResponse();

    /** @var SalesChannelContext */
    $salesChannelContext = $response->getContext();

    $customer = $salesChannelContext->getCustomer();

    if(!$customer)
    {
	 $response = new RedirectResponse($this->router->generate('frontend.home.page'), 301);

	    $event->setResponse($response);
    }

	return true;
}

```

---

<div class="post-metadata">

### Author: ![lucamario](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/lucamario/32/15269_2.png) [@lucamario](https://forum.shopware.com/u/lucamario)
#### Post date: [17. Dezember 2021 um 11:13 UTC](https://forum.shopware.com/t/how-to-redirect-to-home-page-from-a-subscriber-file/65104/5 "2021-12-17T11:13:25Z")

</div>

Works perfect, thank you @abinjohnedamana !
