Question

I want to get URL like this after domain URL

domainname/hellowword/example-index
Was it helpful?

Solution

Try This code

protected $_storeManager;

public function __construct(
    ..........................................................
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    ..........................................................
) {
    ..........................................................
    $this->_storeManager = $storeManager;
    ..........................................................
}

public function getCustomiseUrl()
{
    $url = $this->_storeManager->getStore()->getUrl('hellowword/example-index');
    //$url = $this->_storeManager->getStore()->getCurrentUrl();
    // get prase url
    $prse_url = parse_url($url);
    $get_url = '';
    if (isset($prse_url['host'])) {
        $get_url = $prse_url['host'];
    }
    if (isset($prse_url['path'])) {
        $get_url .= $prse_url['path'];
    }
    echo "get_url ".$get_url;
}

I Hope This Helps You.

OTHER TIPS

You can try to create a custom router (https://devdocs.magento.com/guides/v2.4/extension-dev-guide/routing.html)

or create a URL redirect from the admin URL rewrites to point your hellowword/example-index to your action controller (https://docs.magento.com/user-guide/marketing/url-rewrite-custom.html)

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top