0

I have a module with 2 themes for mobile and desktop.

In the mobile theme I want to change the default homepage to another url.

So when the mobile detection it is activated instead to go to the default page to go to my module route magento.com/mylink

I can put a redirect in the a block and define this block in cms_index_index.xml but looks a bit "dirty".

Is it a way to reroute the mobile theme homepage directly ?

2 Answers 2

1

Get the mobile detect class from:

https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.php

I put it inside my helper folder and just changed the name, namespace and extended as a helper:

namespace Wirelesslogic\ComesConnected\Helper;
class MobileDetect extends \Magento\Framework\App\Helper\AbstractHelper
{
...

Create a module and add the following line in the di.xml file inside etc:

<preference for="Magento\Cms\Controller\Index\Index" type="Wirelesslogic\ComesConnected\Controller\Cms\Index" />

Following this sample create a folder call Cms in the controller folder with a index.php and add the following code:

<?php
namespace Wirelesslogic\ComesConnected\Controller\Cms;
use Magento\Framework\Controller\ResultFactory;
use Wirelesslogic\ComesConnected\Helper\MobileDetect;
use \Magento\Framework\App\Action\Action;
use \Magento\Framework\App\Action\Context;

class Index extends Action

{
    protected $resultFactory;
protected $mobileDetect;

public function __construct(
    Context $context,
    ResultFactory $resultFactory,
    MobileDetect $mobileDetect
)
{
    $this->resultFactory = $resultFactory;
    $this->mobileDetect = $mobileDetect;
    parent::__construct($context);
}

public function execute($coreRoute = null)
{
    $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
    if ($this->mobileDetect->isMobile()) {
        $resultRedirect->setPath('activate');
        return $resultRedirect;
    } else {
        $pageId = $this->_objectManager->get(
            \Magento\Framework\App\Config\ScopeConfigInterface::class
        )->getValue(
            \Magento\Cms\Helper\Page::XML_PATH_HOME_PAGE,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
        $resultPage = $this->_objectManager->get(\Magento\Cms\Helper\Page::class)->prepareResultPage($this, $pageId);
        if (!$resultPage) {
            /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
            $resultForward = $this->resultForwardFactory->create();
            $resultForward->forward('defaultIndex');
            return $resultForward;
        }
        return $resultPage;
    }
    }
}

Clear cache, compile or what ever you need to do depending your env.

Work with routes some times require to delete some var folders so if is not responding delete the var content.

0

I believe the best way in this case will be using custom router. Here is an article about routing in Magento2 . Here is how to detect mobile And how to redirect... I think you'll easily google it. If no, let me know, I'll add this part

1
  • Hi Furman, I don't think that using HTTP_USER_AGENT it is the best way to detect different devices, I used modernizer and other libraries in the past is not that what i am asking. Magento has already a way to assign themes to devices so i don't see the point to add another library and business logic to do that. What i need Is a way to reroute the default home page to another URL. In the Link you sheared shows how to create your own route but what i need it is to override the default home Mar 6, 2018 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.