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.