Jump to content

How to access a custom controller from my module? Controller not found


Recommended Posts

Hello,  I would like to know how to update the view in the backoffice using a controller from a module. I have made this controller and view, code is below:

 

AdminModlistController.php in customizeproducts/controllers/admin

<?php
class AdminModlistController extends ModuleAdminController
{
	public $ssl = true;

	public function __construct()
	{
		parent::__construct();
		$this->context = Context::getContext();
	}

	public function initContent()
	{
		parent::initContent();
		$this->context->smarty->assign(array());
		$this->setTemplate('modlist.tpl');
	}
}
 ?>

AdminModlist.tpl in customizeproducts/views/templates/admin

<div class="panel">
  <h3>
    Hello
  </h3>
</div>

How can I access this controller? Any help would be greatly appreciated.

Link to comment
Share on other sites

You actually need to create a TAB during the installation of your module.

 

If you create a tab with parent attribute at -1, you are creating an invisible tab. Parent at 0, means a firstlevel tab. Parent at X, means your tabl is a sub tab of parent tab id X.

 

to create a new tab, put something like this in the install function of your module :

$class = 'AdminModlist';
$tab = new Tab();
$tab->class_name = $class;
$tab->module = $this->name;
$tab->id_parent = 0;
$langs = Language::getLanguages(false);
foreach ($langs as $l) {
    $tab->name[$l['id_lang']] = $this->l('Mod List');
}
$tab->save();

And reset your module. This will create a TAB called "Quote". This will lead to your controller.

 

To use a tab in parent -1, you need to create a link to it somewhere (Usually in your module configuration) the HREF will look like "$link->getAdminLink("AdminModlist")"

 

This sums it up for now...

 

Martin.

Edited by Martin Uker K (see edit history)
  • Like 1
Link to comment
Share on other sites

Thank you very much, Martin!

 

I added the code to my install method and I found a tab named "Mod List", on clicking it, I got this error message, "Fatal error: Uncaught Error: Class 'AdminModlistcontrollerController' not found".

 

I got the same error on clicking the href=$link->getAdminLink("AdminModlist").

 

I tried changing the name of the controller to AdminModlistcontrollerController and on clicking the tab, I got a prestashop exception "Admin tab AdminModlistcontrollerController is not a module tab" .

 

Clicking on the href takes me to a controller not found page.

Link to comment
Share on other sites

after placing the view in the correct folder, I got the required result. Here is my code now:

 

customizeproducts/controllers/admin/AdminModlistController.php:

<?php
class AdminModlistController extends ModuleAdminController
{
	public $ssl = true;

	public function __construct()
	{
		parent::__construct();
		$this->context = Context::getContext();
	}

	public function initContent()
	{
		parent::initContent();
		$this->context->smarty->assign(array());
		$this->setTemplate('modlist.tpl');
	}
}
 ?>

moved the view from views/admin to views/admin/modlist

 

Thank you, Martin.

  • Like 1
Link to comment
Share on other sites

  • 8 months later...
  • 2 months later...
  • 5 years later...

Easiest solution for this would be to reinstall/reset module.

 

If that's not possible or hard (you don't want to loose DB data or for any other reason), then there is another way.

Those module backoffice controller page informations are stored in ps_tab + ps_tab_lang, while module front controllers are stored in ps_meta + ps_meta_lang tables, one could add it manually, but it is not easy, you need to also add permissions to access it, so I suggest not doing that.

In general afaik the logic in PrestaShop is that on install there are two related methods executed, for frontend controllers: class `Module`, method `installControllers`, and for BackOffice controllers there is an event subscriber class called `ModuleTabManagementSubscriber`, and method `onModuleInstall`, which executes method `registerTabs` of class `ModuleTabRegister` that contains all the logic for adding new missing Tabs to DB.

 

I wrote custom logic to reset module without loosing data. You need to add:

1 to `Module` class (best is to do override):

 /**
     * Refresh module's controllers using public property $controllers.
     *
     * @return bool
     */
    public function refreshControllers()
    {
        $moduleManagerBuilder = ModuleManagerBuilder::getInstance();
        $moduleManager = $moduleManagerBuilder->build();
        $moduleManager->reset( $this->name,true);

        foreach ($this->controllers as $controller) {
            $page = 'module-' . $this->name . '-' . $controller;
            $result = Db::getInstance()->getValue('SELECT * FROM ' . _DB_PREFIX_ . 'meta WHERE page="' . pSQL($page) . '"');
            if ((int) $result > 0) {
                continue;
            }

            $meta = new Meta();
            $meta->page = $page;
            $meta->configurable = 1;
            $meta->save();
        }

        return true;
    }

2 new use statement on top to `Module` class:

use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder;

3. new empty reset method to your module file:


    public function reset() {
    }

4 and then add to your module file inside getContent() method this function execution:

	public function getContent()
	{
		// execute only once, and then remove
		$this->refreshControllers();

		...
	}

Execute it only by visiting your module configuration page. Comment it out afterwards. You can use it later to easily refresh controllers once new are added.

I know this answer could be just a first sentence, but wanted to share my (not ideal) solution here as well. I have spent "some" time doing research for this.

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...