Question

I make a Block in my custom module and there a logic exist . I have also make controller ,xml file(for define block class and template file) ,template in Controller nothing custom code exist just pagefactory exist in execute method. Now Path is domain.com\routerid\controller\action

But I need domain.com

How can i make domain.com\routerid\controller\action on landing page ???

Était-ce utile?

La solution

Try using router, the following example will forward home request to contact page, change that line. So when you browse home page, that will display contact page.

app/code/SR/MagentoCommunity/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\RouterList">
        <arguments>
            <argument name="routerList" xsi:type="array">
                <item name="custom" xsi:type="array">
                    <item name="class" xsi:type="string">SR\MagentoCommunity\Controller\Router</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">10</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

app/code/SR/MagentoCommunity/Controller/Router.php

<?php
namespace SR\MagentoCommunity\Controller;

use Magento\Framework\App\RouterInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ActionFactory;

class Router implements RouterInterface
{
    /**
     * @var ActionFactory
     */
    private $actionFactory;

    /**
     * Router constructor.
     *
     * @param ActionFactory $actionFactory
     */
    public function __construct(
        ActionFactory $actionFactory
    ) {
        $this->actionFactory = $actionFactory;
    }

    /**
     * @param RequestInterface $request
     * @return \Magento\Framework\App\ActionInterface|null
     */
    public function match(RequestInterface $request)
    {
        $identifier = trim($request->getPathInfo(), '/');
        if (!$identifier && !$request->getControllerName()) {
            $request->setModuleName('contact')->setControllerName('index')->setActionName('index');
            $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, 'contact');
            $request->setDispatched(true);
            return $this->actionFactory->create(\Magento\Framework\App\Action\Forward::class);
        }

        return null;
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top