Reading list Switch to dark mode

    How to create a custom API endpoint instead of api in webservice using PrestaShop 1.7

    Updated 14 June 2023

    In this blog, We are going to learn how we can create a custom API endpoint instead of api in the PrestaShop webservice. Sometimes we want to show our custom endpoint in webservice URL instead PrestaShop existing endpoint (api).

    We are creating a custom module to manage things on the install/uninstall process of this module.

    For our example, we have created a .txt file with the code which we want to add in core PrestaShop files. We have created an update_htaccess.txt file with this below code & we will add some custom code in the .htaccess file at the time of module installation.

    RewriteRule ^api(?:/(.*))?$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
    RewriteRule ^customapi/v1(?:/(.*))?$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
    Selection_185

    Check the below code which we have added to the install function.

    public function install()
    {
        if (!parent::install()
            || !$this->addCustomCodeInHtaccessFile()
        ) {
            return false;
        }
    
        return true;
    }
    
    private function addCustomCodeInHtaccessFile()
    {
        $findInHtaccess = 'RewriteRule ^api(?:/(.*))?$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]';
        $updatedContent = file_get_contents(_PS_MODULE_DIR_. $this->name . '/update_htaccess.txt');
        $htaccessFile = _PS_CORE_DIR_.'/.htaccess';
        $htaccessContent = file_get_contents($htaccessFile);
        $updatedHtaccessContent = str_replace($findInHtaccess, $updatedContent, $htaccessContent);
        file_put_contents($htaccessFile, $updatedHtaccessContent);
    }

    Now you can see when the module is installed then customapi endpoint related code is added in the main .htaccess file.

    Searching for an experienced
    Prestashop Company ?
    Find out More
    Selection_186

    The same flow you can use for the uninstall process that when the module will uninstall then custom code should be removed from the .htaccess file.

    public function uninstall()
    {
        if (!parent::uninstall()
            || !$this->removeCustomCodeFromHtaccessFile()
        ) {
            return false;
        }
    
        return true;
    }
    
    private function removeCustomCodeFromHtaccessFile()
    {
        $findInHtaccess = 'RewriteRule ^customapi/v1(?:/(.*))?$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]';
        $htaccessFile = _PS_CORE_DIR_.'/.htaccess';
        $htaccessContent = file_get_contents($htaccessFile);
        $updatedHtaccessContent = str_replace($findInHtaccess, '', $htaccessContent);
        file_put_contents($htaccessFile, $updatedHtaccessContent);
    }

    Now when you will run the webservice with a custom endpoint then it will work the same as the core api endpoint.

    Selection_184

    Note: If you can change the functionality or flow then you can add a custom dispatcher file for your custom api endpoint. Here is an example.

    RewriteRule ^customapi/v1(?:/(.*))?$ %{ENV:REWRITEBASE}webservice/customdispatcher.php?url=$1 [QSA,L]

    Then you need to add a customdispatcher.php in webservice folder.

    That’s all about this blog. Hope it will help you.

    If you are facing any issues or have any doubts about the above process, please feel free to contact us through the comment section.

    Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected]

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home