ftardio Posted April 6, 2019 Share Posted April 6, 2019 (edited) Hi! I'm working in a module. It has a front controller for making some processes through URL calls. Now, I want to use some methods of this controller from its parent module (directly, not getting a URL link). But I don't know how to get an instance of my front controller from the module class. I'll put some brief coding to better explain: My module: class MyModule extends Module { public function __construct() { $this->name = 'mymodule'; $this->controllers = array('myFrontController'); [...] } } And now my controller: class MyFrontControllerModuleFrontController extends ModuleFrontController { public function initContent() { [...] } protected function myOwnProcess() { [...] } } The goal is to execute myOwnProcess() from my module, and this is why I first need to get an instance to the controller object (and I don't know how to get this). Any help? Thanks in advance. Edited April 6, 2019 by ftardio (see edit history) Link to comment Share on other sites More sharing options...
Janett Posted April 6, 2019 Share Posted April 6, 2019 In your ModuleFrontController you can access to your module instance with $this->module so you can do $this->module->myOwnProcessInMyModule() if your have a public function named myOwnProcessInMyModule in your module if you want access to a function inside your ModuleFrontController when you are in your Module class, you have to create an instance of this controller Link to comment Share on other sites More sharing options...
ftardio Posted April 6, 2019 Author Share Posted April 6, 2019 2 minutes ago, Janett said: In your ModuleFrontController you can access to your module instance with $this->module so you can do $this->module->myOwnProcessInMyModule() if your have a public function named myOwnProcessInMyModule in your module if you want access to a function inside your ModuleFrontController when you are in your Module class, you have to create an instance of this controller Thanks for your reply, Janett. I already know you can use module methods from a controller using $this->module object, but I need to do the opposite, that is to use a method created at the controller from its parent module. As you say, I need to create a instance of this controller first, but... how? Link to comment Share on other sites More sharing options...
Janett Posted April 6, 2019 Share Posted April 6, 2019 (edited) /** * Get an instance of a ModuleFrontController * * @param string $controller The name of the controller, it must be declared in your module * * @return ModuleFrontController Instance of the controller */ public function getModuleFrontControllerInstance($controller) { // Check this controller is allowed if (!in_array($controller, $this->controllers)) { throw new PrestaShopException(sprintf('This controller %s is not declared in this module', Tools::safeOutput($controller))); } // Include the controller file require_once __DIR__ . '/controllers/front/' . $controller . '.php'; // Build dynamically the controller name $controller_name = $controller . 'ModuleFrontController'; // Instantiate controller $controller = new $controller_name(); // Return the controller return $controller; } public function weirdFunction() { $controller = $this->getModuleFrontControllerInstance('myFrontController'); return $controller->myOwnProcessInModuleFrontController(); } Try somethings like that Edited April 6, 2019 by Janett (see edit history) 1 1 Link to comment Share on other sites More sharing options...
ftardio Posted April 6, 2019 Author Share Posted April 6, 2019 (edited) Thank you very much for your help, Janett. The method you proposed is an "object oriented" way (first include the controller class, and then instantiate it and create a new object). It'll work, sure. But... there are not any "prestashop method" for this? Using context object, or maybe some Prestashop classes? I have been searching forums, and they talk about using "$this->getController('nameOfController')" at the module, but it doesn't work, I don't know if this worked in some previous Prestashop version, and they moved the method to another class in more recent versions. Edited April 6, 2019 by ftardio (see edit history) Link to comment Share on other sites More sharing options...
Janett Posted April 6, 2019 Share Posted April 6, 2019 Problem is method in Dispatcher class is mostly private. What's your version of PrestaShop ? Link to comment Share on other sites More sharing options...
ftardio Posted April 6, 2019 Author Share Posted April 6, 2019 1 minute ago, Janett said: Problem is method in Dispatcher class is mostly private. What's your version of PrestaShop ? I'm using 1.6.12 version. Link to comment Share on other sites More sharing options...
Janett Posted April 6, 2019 Share Posted April 6, 2019 So use the method I explained, nothing usefull in this case in PrestaShop classes. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now