0

I am in the process of developing a Custom Module for my Magento 2 web store.

The module will be a forum, I am developing this from scratch as it will require integrating with an existing custom module I have developed to share user created content.

In the forum, I wish to have URLs specific to each Post/Thread. For example:

www.mywebsite.com/Forum/Category/PostTitle

This will keep the urls clean, allowing users to quickly access a specific post or category without navigating through the website.

The data will be stored in our magento database and so the variables for the URL will need to come from there.

An example of what i wish to implement is also used on Stack Exchange; The url for this question is:

https://magento.stackexchange.com/questions/209504/custom-module-dynamic-url

Where the last part of the url custom-module-dynamic-url is the title of my question.

I have drawn up a rough diagram of what I wish to create.

https://repository.genmymodel.com/darryl3/Forum

How would I go about implementing such a feature?

Thanks.

0

You can find the same thing in module-cms in this module we are adding text for URL but I think you can easily convert into with small changes. In module-cms you can see that they have created custom router list so you also need to do the same thing. For example,

app/code/yourvendor/yourmodule/etc/frontend/di.xml

In this file, you need to add

YOURVENDOR\YOURMODULE\Controller\Router false 60

Now you need to create your router YOURVENDOR\YOURMODULE\Controller\Router so it can catch every request but you need to make sure that it implements Magento\Framework\App\RouterInterface

     namespace YOURVENDOR\YOURMODULE\Controller;

    class Router implements \Magento\Framework\App\RouterInterface
    {

     /**
     * @var \Magento\Framework\App\ActionFactory
     */
    protected $actionFactory;

    /**
     * Event manager
     *
     * @var \Magento\Framework\Event\ManagerInterface
     */
    protected $_eventManager;

    /**
     * Store manager
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;


    /**
     * Config primary
     *
     * @var \Magento\Framework\App\State
     */
    protected $_appState;

    /**
     * Url
     *
     * @var \Magento\Framework\UrlInterface
     */
    protected $_url;

    /**
     * Response
     *
     * @var \Magento\Framework\App\ResponseInterface
     */
    protected $_response;


  public function __construct(
        \Magento\Framework\App\ActionFactory $actionFactory,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Framework\UrlInterface $url,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\ResponseInterface $response
    ) {
        $this->actionFactory = $actionFactory;
        $this->_eventManager = $eventManager;
        $this->_url = $url;        
        $this->_storeManager = $storeManager;
        $this->_response = $response;
    }

     public function match(\Magento\Framework\App\RequestInterface $request)
       {
        $identifier = trim($request->getPathInfo(), '/');

        $condition = new \Magento\Framework\DataObject(['identifier' => $identifier, 'continue' => true]);

        $identifier = $condition->getIdentifier();

        if ($condition->getRedirectUrl()) {
            $this->_response->setRedirect($condition->getRedirectUrl());
            $request->setDispatched(true);
            return $this->actionFactory->create('Magento\Framework\App\Action\Redirect');
        }

        if (!$condition->getContinue()) {
            return null;
        }

        // check your custom condition here if its satisfy they go ahed othrwise set return null
        $satisfy=true;
        if (!$satisfy) {
            return null;
        }

        $request->setModuleName('yourmodule')->setControllerName('yourcontroller')->setActionName('youraction')->setParam('custom_id',"test" );
        $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $identifier);

        return $this->actionFactory->create('Magento\Framework\App\Action\Forward');
        }
    }

In this code, I added 1 flag $satisfy so in this place you can add your custom condition.

If its match then you can see It redirect into your custom controller file.

$request->setModuleName('yourmodule')->setControllerName('yourcontroller')->setActionName('youraction')->setParam('custom_id',"test" );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy