I want to redirect to the home page (xyx.com) using the observer object & i am using the event controller_action_predispatch_cms_index_index
.
below syntax is working fine when moving to the login page by the observer, Even I can move to any custom URL with the help of an observer object but unable to redirect to the home page.
below code is just for example. $observer->getControllerAction()->getResponse()->setRedirect('customer/account/login');
I have tried many solutions:
Try Solution 1 :
$observer->getControllerAction()->getResponse()->setRedirect('home');
but it will redirect to xyz.com/home
Try Solution 2:
(a) $Url = $this->url->getUrl('');
OR (b) $Url = $this->url->getBaseUrl();
or (c) $Url = $this->url->getUrl('/');
& pass $Url to setRedirect but that is also not working.
$observer->getControllerAction()->getResponse()->setRedirect($Url);
My Question is how to pass home page address to setRedirect.
Updated: Don't know the getUrl behaviour. Everthing thing is working & even custom url also working on getUrl on passing on observer & i am able to redirect anywhere accpording to my wish.
$url = getUrl('custom/url') or getUrl('home')
or getUrl('customer/account/login')
all the above are working on setRedirect => $observer->getControllerAction()->getResponse()->setRedirect($url);
but $url = getUrl() is not working.
Does anyone face the same issue on redirection from observer. ?