1

Is this possible to get admin path url in custom module controller? How can I get for Magento 2x? there I don't have default admin in path.

<?php
/**
 * Copyright © 2015 Technologymindz. All rights reserved.
 */

namespace Technologymindz\Instagramfeed\Controller\Adminhtml\Insta;
use Magento\Framework\Controller\ResultFactory;

class Auth extends \Technologymindz\Instagramfeed\Controller\Adminhtml\Insta
{
   public function __construct(
        //other stuff you need here
        \Magento\Framework\App\DeploymentConfig\Reader $configReader
    ) {
        $this->configReader = $configReader;
        //other stuff you need here, like parent::__construct($context, $data);
    }
    /**
     * Items list.
     *
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {

                $config = $this->configReader->load();
                $adminSuffix = $config['backend']['frontName'];
        echo   $adminUrl = $this->_storeManager->getStore()->getBaseUrl() . $adminSuffix;
        die;


    }
}

2 Answers 2

1

Use below code:

public function __construct(
        //other stuff you need here
        \Magento\Framework\App\DeploymentConfig\Reader $configReader
    ) {
        $this->configReader = $configReader;
        //other stuff you need here, like parent::__construct($context, $data);
    }

Use below line to get admin url:

$config = $this->configReader->load();
$adminSuffix = $config['backend']['frontName'];
$adminUrl = $this->_storeManager->getStore()->getBaseUrl() . $adminSuffix;
5
  • This give me error: Fatal error: Call to a member function isLoggedIn() on null in /var/www/html/magento2/vendor/magento/module-backend/App/AbstractAction.php on line 252 what about this?
    – Sohan
    Commented Jun 14, 2016 at 7:44
  • Put your full code in your question Commented Jun 14, 2016 at 7:45
  • check the update, i have added your code in that too
    – Sohan
    Commented Jun 14, 2016 at 7:52
  • this error is not from this file may be its coming from other code so please make sure Commented Jun 14, 2016 at 8:39
  • i did check before use this code all is working to me.
    – Sohan
    Commented Jun 14, 2016 at 11:27
0

You can get the admin URL by making use of the helper class, \Magento\Backend\Helper\Data

Change your controller file like the following,

<?php 
namespace Technologymindz\Instagramfeed\Controller\Adminhtml\Insta;

use Magento\Framework\Controller\ResultFactory;

class Auth extends \Technologymindz\Instagramfeed\Controller\Adminhtml\Insta
{

public function __construct(
\Magento\Framework\App\DeploymentConfig\Reader $configReader,
\Magento\Backend\Helper\Data $HelperBackend )
{
    $this->configReader = $configReader;
    $this->HelperBackend = $HelperBackend;
    //other stuff you need here, like parent::__construct($context, $data);
}

 public function execute()
 {
    echo $this->HelperBackend->getHomePageUrl();
    die;
 }
}

Run the setup upgrade command and check the result.

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.