Magento – how do use UrlRewrite for Custom module in magento2

magento2magento2-dev-beta

I have custom a module.
When I do getUrl, it shows:

http://localhost/magento2_beta/index.php/faq/category/view/tags/abc/

So I want an url like: http://localhost/magento2_beta/tag.phtml

How to do it?

Best Answer

You may use native rewrite functionality by using menu Marketing->(SEO & Search)->Url Rewrites

Or if you want to implement your own advanced routing - Magento 2 allows to declare your own routers:

Create your router that implements Magento\Framework\App\RouterInterface

Declare your router as additional argument for Magento\Framework\App\RouterList in your di.xml.

<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="router_unique_code" xsi:type="array">
                <item name="class" xsi:type="string">Vendor\Module\Controller\Router</item>
                <item name="disable" xsi:type="boolean">false</item>
                <item name="sortOrder" xsi:type="string">100500</item>
            </item>
        </argument>
    </arguments>
</type>

See example: https://github.com/magento/magento2/blob/merchant_beta/app/code/Magento/Cms/Controller/Router.php

https://github.com/magento/magento2/blob/merchant_beta/app/code/Magento/Cms/etc/frontend/di.xml