0

I am using Magento 2 Magefan blog extension. The blog post URL is generated as below.

http://store-url/blog/post/hurry-xmas-with-family

But I want URL as below and it show the content of the above URL

http://store-url/hurry-xmas-with-family

How can this be achieved? Apart from using URL rewrite is there any other method? I was thinking of creating a custom router but I think it will conflict with CMS router.

1 Answer 1

0

You need to create Router.php in your Controller, get any URL and manipulate in as you want. For your understanding, I'm attaching a code snippet of match function inside Router.php. If you find any trouble understanding it, you can ask.

public function match(RequestInterface $request): ?ActionInterface{
        $identifier = trim($request->getPathInfo(), '/');
        $url_parts = explode("/", $identifier, 4);
        if (isset($url_parts[1]) == false && (strpos($identifier, $this->helperData->getStorefrontConfig('url_title')) !== false)) {
            $request->setModuleName('article');
            $request->setControllerName('index');
            $request->setActionName('index');

            return $this->actionFactory->create(Forward::class, ['request' => $request]);
        } elseif ((isset($url_parts[1]) == true) && ($url_parts[0] == $this->helperData->getStorefrontConfig('url_title'))) {
            $collection = null;
            if (isset($url_parts[2])) {
                $collection = $this->articleFactory->create()->load($url_parts[2], 'page_id');
            }
            $id = $collection->getId();

            $request->setModuleName('article');
            $request->setControllerName('index');
            $request->setActionName('view');
            $request->setParams([
                'id' => $id
            ]);
            return $this->actionFactory->create(Forward::class, ['request' => $request]);
        }
        return null;
    }```

Your Answer

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

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