Override Prestashop Core controller using a module - prestashop

Working on a project that consist to create thumbs for the product's features,
is there a way to override the core controller *AdminFeaturesController*using a module?

In your module you can create a /override directory. Under this directory you will create this file /override/controllers/admin/AdminFeaturesController.php.
This file should look as follow:
<?php
class AdminFeaturesController extends AdminFeaturesControllerCore
{
// Only override the functions that should be modified
public function function_that_i_want_to_override() {
}
}
The override will be effective after module installation.

Related

Override and use front controller features in a prestashop module [1.6.x.x]

I would like to edit and add features to the prestashop Store Locator page.
Prestashop's documentation isn't really clear, and i would like to know if it's possible to implement a Controller in a custom module.
I would like to create a module which is able to extends StoreFrontController and it's features without starting from scratch.
Is it possible ? Have you some documentation for me ?
A beginner,
Best.
As you have many requirements, you will have to go with an override of class StoresController.php.
Your module folder should look like this:
/mymodule
/mymodule.php
/config.xml
/override
/controllers
/front
StoresController.php
/views
/templates
/front
stores.tpl
In StoresController.php you will have to override initContent():
<?php
class StoresController extends StoresControllerCore
{
/**
* Assign template vars related to page content
* #see FrontController::initContent()
*/
public function initContent()
{
parent::initContent();
// here add any smarty variables you want
$this->setTemplate(_PS_MODULE_DIR_.'mymodule/views/templates/front/stores.tpl');
}
}
Now you can add as many variables as you want in this controller and customized its template in your own module.
We you create an override in a module, it will be only parsed once at installation. If your module is already installed you will have to uninstall it and install it again. Then your override file will be copied to the root /override folder.
Any change made in your module override will not be reflected to the root override folder. So you will have to uninstall and install your module each time you want to make a change.
So I advise you to make all your changes directly in the root override folder, and when you're done copy this file back into your module folder. And if you don't want to uninstall your module and install it again to declare this file, you can put it directly in the root override folder and delete the file /cache/class_index.php so that Prestashop knows that an overrides has been added.
If you have any questions :)
You can start by overriding front controller like
`"/modules/mymodule/override/controllers/front/StoresController.php" and in this fine add class "class StoresControllerCore extends FrontController {
public function initContent()
{
parent::initContent();
//here do whatever you like
}
}"
though you must know coding to proceed further.

Prestashop override FrontControllerCore class from custom module

I want to override Prestashop FrontControllerCore class from my custom module. So can someone tell me how to override FrontControllerCore class from custom module. I am using Prestashop 1.6.
In your module add a new file
override/classes/controller/FrontController.php
and add this content
class FrontController extends FrontControllerCore
{
//.. override methods
}

Shared base controller between modules

I am setting up a multi-module application, so far I have it setup like this example http://docs.phalconphp.com/en/latest/reference/applications.html.
But I was wandering if its possible to have shared base controller that both the backend and frontend controllers extend from. This is so I can have a single ACL in the base controller. How would I set that up?
According to the docs I can create a controllerbase anywhere and then just require this file directly in the bootstrap file or cause to be loaded using any autoloader. So I created a folder called apps/shared/controllers/ControllerBase.php and required this file directly in the bootstrap file but this does not work.
If I try to load a controller like so:
class AdminController extends ControllerBase
{
public function indexAction()
{
echo "<h1>Hello admin!</h1>";
}
}
I get an error ...Backend\Controllers\ControllerBase' not found in......
So how do I cause to be loaded using any autoloader as per the docs? Do I need to register it as its own namespace or something?
You not using the full namespace path for your base controller so the autoloader attempts to find it under in the same namespace of the child class.
Try something like this:
namespace MyApp\Backend\Controllers;
use MyApp\Shared\Controllers\ControllerBase;
class AdminController extends ControllerBase
{
public function indexAction()
{
echo "<h1>Hello admin!</h1>";
}
}
This answer consider that you have applied the PSR-0 and PSR-4 properly.

How to add javascript in a prestashop module

Is it possible to integrate javascript in a module without a direct injection in the template smarty?
Solved ,
I added a Js file in my module directory (ex : mymodule/views/js/abo_front.js).
In my module class, I created a Hook to the header to declare my js file in the header.
public function install()
{
return parent :: install() && $this->resetDb()
&& $this->registerHook('header');
}
public function hookHeader($params)
{
$this->context->controller->addJS(($this->_path).'views/js/abo_front.js');
}

How to template prestashop module admin section with smarty

I want to build a module where , we like to build the form and table using smarty.
In prestashop module controller load template file like
/modules/my_module/views/templates/front/my_module.tpl
where admin will be /modules/my_module/views/templates/admin/admin_module.tpl
My point is how can i show this admin_module.tpl in the prestashop module configuration page.
It is really easy. You just have to create views/templates/admin/foo.tpl and then only display your template in getContent() method:
public function getContent()
{
return $this->display(__FILE__, 'views/templates/admin/foo.tpl');
}