I figure it out to get dynamic url in the front end if I need I can simply define mage/url and the use it as url.build('<Modulename>/<controllername>/<action>/')

When I tried the same thing for the backend it didn't return me the expected result.

It gave the the current url and append the path which I had given.

Can anyone explain how this works out. Do I need to define the baseUrl somewhere ?


Thanks to Khoa for his explanation what I wanted to achieve is in my admin form I am calling an external url with ajax. I was using a static path so I wanted it dynamically. So here's what I have done.

My Layout XML File

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
                <uiComponent name="namespace_modulename_form"/>
                <block class="Namespace\Modulename\Block\Adminhtml\Edit" name="edit" template="Namespace_Modulename::edit.phtml" />

Layout Phtml File I defined the baseUrl.

        ], function(url) {
            return url.setBaseUrl('<?php /* @escapeNotVerified */ echo $block->getAdminBaseUrl();?>');

My block file which returns the base url


namespace Namespcae\Modulename\Block\Adminhtml;

class Edit extends \Magento\Framework\View\Element\Template

    protected $_configReader;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\DeploymentConfig\Reader $configReader
    ) {
        $this->_configReader = $configReader;

    public function getAdminBaseUrl(){
        $config = $this->_configReader->load();
        $adminSuffix = $config['backend']['frontName'];
        return $this->getBaseUrl() . $adminSuffix . '/';

Now in my js files I can use it as.

], function (Abstract, url) {
    'use strict';

    return Abstract.extend({
| improve this answer | |

Just Do That Only in js file

  function (url) {
   'use strict';
    var url = url.build('<modulename>/<controllername>/<actionname>');

Result will be:

| improve this answer | |
  • This will not give me the same result In backend. So need to define the base the url in block first – Priyank Jan 16 '17 at 8:33
  • But in ur question u mention that u want URL in js so this will helpful. Now tell me in which file do u want URL. Is your problem solved or not? – Nitesh Jan 16 '17 at 14:13
  • How we get base url of site using url.build() function? – Prince Patel Jul 24 '17 at 13:21
  • using this -> define(['mage/url'] – Nitesh Jul 24 '17 at 14:52

The most important thing we need to know: We need to set base url for url.build('<Modulename>/<controllername>/<action>/'):


        setBaseUrl: function (url) {
            baseUrl = url;
        build: function(path) {
            if (path.indexOf(baseUrl) != -1) {
                return path;
            return baseUrl + path;

Basically, there are two places where to set the base url:

vendor/magento/module-checkout/view/frontend/templates/onepage.phtml vendor/magento/module-checkout/view/frontend/templates/cart/shipping.phtml

return url.setBaseUrl('<?php /* @escapeNotVerified */ echo $block->getBaseUrl();?>');

So, in your admin, you have to set the admin base url.

| improve this answer | |
  • 1
    Ok I get that. But talking about admin forms, where I need this to be implemented. My form is created using ui component so how Can I set the url as it has been defined in onepage or shipping ? – Priyank Oct 4 '16 at 5:13
  • Hello, you can explain more details about your issue. About Ui component: magento.stackexchange.com/questions/97291/… – Khoa TruongDinh Oct 4 '16 at 5:36

Your Answer

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

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