Jump to content

How to get a instance for a custom controller in my module?


Recommended Posts

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 by ftardio (see edit history)
Link to comment
Share on other sites

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

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

    /**
     * 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 by Janett (see edit history)
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

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 by ftardio (see edit history)
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...