Search code examples
prestashop

How to get Prestashop admin dir & admin url?


i am trying to get admin url and admin dir from a module.

Can you give an example finding admin url and admin dir from a module php page?

<?php
define('PS_ADMIN_DIR', dirname(__file__) . '/../../testadmin');  
echo PS_ADMIN_DIR;

this code gives me

/home/myaccount/public_html/modules/suppliers/../../testadmin

i was expecting to get

/home/myaccount/public_html/testadmin

Any ideas?


Solution

  • You can get it by using " _PS_ROOT_DIR_ " and append your admin dir name with it like below

     _PS_ROOT_DIR_."/testadmin" 
    

    This will give you full path to the admin directory. The _PS_ROOT_DIR_ is used to get the PS complete path to directory where it is installed.

    You can't get the admin url directly, as PS system dont know the admin directory, because we can change the admin directory name at any time to any name for security reasons. So to construct an admin url knowing your admin directory name "testadmin" and the controller name "AdminTest", you can create a url as below

    $link = new Link(); 
    _PS_BASE_URL_.__PS_BASE_URI__."testadmin/".$link->getAdminLink('AdminTest', true);
    

    This will give you complete link including full domain and sub directory (if installed in sub directory).

    the getAdminLink gets two argument , controller name and second is Boolean true / false. By default the second is set to true, so need to pass it. It is used to create the token for the controller.

    I hope this will help you.

    Note : The above code is just for idea, it may or may not need some changes.