How To Get Base Path In Prestashop In Controller and TPL files
In this tutorial I will show you a easy method to get base path in controller and template files, using the following method you will have all control you need on your code, so you can add what changes you need to the url anytime you need. The easiest way to get the base path in prestashop is to make your own function and then call it every time you need. If you need it to be global in controllers you have to do the following steps:
First go in override folder and create the following folders and files:
- override
- controllers
- front
- FrontController.php
- front
- controllers
In FrontController file we will create our function and the just call it in your controller when you need to get the base path of your prestashop. This is the function:
public function base_path(){ $protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://' : 'http://'; return $protocol.$_SERVER['SERVER_NAME']; }
However the above function can be accessed only in the controller but what if we need the base path in the tpl files too? To achive this functionality we need to create a new function in FrontController too. So the function to assign a global variable to all tpl files is the following:
public function init(){ parent::init(); self::$smarty->assign( array( 'base_path' => $this->base_path() ) ) }
This is how you can get all the time base path in controller and in tpl files too. Let me know if this method worked for you.