Jump to content

How To Call Module's Controller Method In Module.


Recommended Posts

Hello,

 

I'm trying to create a module and so far everything is working as intended, my controller works when linked on it's own and any calls using $this->context->link->getAdminLink('MyAdminController') work as intended, however my module's .php file has a hook declared within that needs to call a method that's found in my module's /controllers/admin/MyAdminController.php file.

 

I dont know and havn't been able to figure out using google or by reading prestashop source code how to call the controllers method from within the modules .php.

 

I have a controller in prestashop_root/module_dir/mymodule/controllers/admin/MyAdminController.php

 

and I have a module in prestashop_root/module_dir/mymodule/MyModule.php

 

I have a hook in MyModule.php and within that hook I'm trying to call the public function test which is found in MyAdminController.php and the return value is a string that will then ideally be displayed wherever the hook is called.

 

Problem I'm having is things like getInstanceByName and getController functions called in the MyModule.php hook function to get an instance of the controller so I can access the test method are not working.

 

I can't help but feel like I'm missing the obvious or that there is another way to achieve what I need to without having to instantiate the controller to call the method.

 

Thanks in advance!

Alex

Edited by a1eio (see edit history)
Link to comment
Share on other sites

Thanks for response but I'm not sure that will work.

 

To clarify what I'm trying to accomplish:

 

I have a module thats using the hook: hookAdminStatsModules. the return value of this hook needs to be some html that is then displayed in the AdminStats page.

 

What I'm trying to do is call a public function called 'test' which is found in my module's controller: module_dir/mymodule/controllers/admin/MyAdminController.php

 

The problem I'm having is figuring out how to get an instance of that controller loaded into my module: module_dir/mymodule/mymodule.php that I can call.

 

Thinks like: AdminController::getController('MyAdminController'); or Module::getInstanceByName('MyAdminController'); or new MyAdminController(); do not return the controller I've got located in:

module_dir/mymodule/controllers/admin/MyAdminController.php

 

Edit: I've noticed it's easy enough to call a module's method from within the controller using: $this->module->methodName. But I'm trying to do the reverse. I want to call a controller's method from within the module.

 

example psuedo code: $this->controller->test(); returns html and is called from within MyModule.php

Edited by a1eio (see edit history)
Link to comment
Share on other sites

The controller I've got extends ModuleAdminController and handles all the display information needed when viewing said controller through the link: index.php?controller=MyAdminController

 

However I want the hook thats declared in: module_dir/mymodule/mymodule.php to display the same content as: index.php?controller=MyAdminController

 

This hook is called by the AdminStatsController when viewing the back office stats page.

 

So to do this I'm trying to get an instance of MyAdminController in mymodule.php so that I can call MyAdminController::test() which will return the same html displayed when the function MyAdminController::initContent() is called when the link: index.php?controller=MyAdminController is loaded in the browser.

 

It's not my controller thats calling the hook but some prestashop controller. And to be honest I dont really need the hook anymore as my content is displayed successfully using it's own controller. But the reason i'm trying to keep the hook is so that my content is displayed both in the admin stats page aswell as it's own page.

 

If I was to move the hook: hookAdminStatsModules from the mymodule.php file to: mymodule/controllers/admin/MyAdminController.php how would I register it in the mymodule.php install?

 

At the moment mymodule.php contains:

 

public function install()

{

return parent::install && $this->registerHook('AdminStatsModules')

}

 

public function hookAdminStatsModules($params)

{

$html .= MyAdminController::test();

return $html

}

 

Obviously this is not working :)

Edited by a1eio (see edit history)
Link to comment
Share on other sites

Thanks a bunch for the help, just to finalize my hook now uses the same tpl as my controller and there is no unnecessary repeating of code.

 

my hook now looks like this:

  public function hookAdminStatsModules($params)
  {
    $controller_link = $this->context->link->getAdminLink('MyAdminController', array(), true);
    $this->context->controller->addCSS(__PS_BASE_URI__.'modules/'.$this->name.'/css/'.$this->name.'.css');
    $this->context->smarty->assign(array_merge($this->generateTables(), array('controller_link' => $controller_link)));
    return $this->display(__FILE__, 'fullscreen.tpl');
  }

 

and returns the same content whether called in the statspage using the hook, or when viewing my controller directly.

 

Thanks again for the help and I'm sure I'll be back with more questions at some point soon :)

 

Regards

Alex

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...