Jump to content

Module - controller is never recognised


Recommended Posts

Prestashop 8.2, trying to add a controller to a module.

File, called AdminYourCustomController.php in the controllers/admin directory of the module. Defined as 

class AdminYourCustomController extends ModuleAdminController{...}

called from my module

            $this->context->smarty->assign([
                'my_url' => $this->context->link->getAdminLink('AdminYourCustomController', true, [], ['action' => 'synchronizeProducts']),
            ]);

module uninstalled, reinstalled, cache cleared, read/write rights to the controller file checked and the controller is not recognised when called either by getAdminLink or a direct call to the URL of the controller.

I've seen on some old posts that a tab needed to be added and initialised in the install() method. I tried this but it didn't do anything.

Does anyone have a working solution for adding a controller in 8.2 ?

Link to comment
Share on other sites

Yes, you need to add tab in installation function like this:

        // add admin cotroller support
        if (!$this->existsTab($this->tab_class)) {
            if (!$this->addTab($this->tab_class, -1)) {
                return false;
            }
        }
        // #add admin cotroller support
    public function existsTab($tab_class)
    {
        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT id_tab AS id
        FROM `' . _DB_PREFIX_ . 'tab` t WHERE LOWER(t.`class_name`) = \'' . pSQL($tab_class) . '\'');
        if (count($result) == 0) {
            return false;
        }

        return true;
    }

where $tab_class = 'AdminYourModuleName';

    private function addTab($tab_class, $id_parent)
    {
        $tab = new Tab();
        $tab->class_name = $tab_class;
        $tab->id_parent = $id_parent;
        $tab->module = $this->tab_module;
        $tab->name[(int) Configuration::get('PS_LANG_DEFAULT')] = $this->l('Any simple name for module');
        $tab->add();

        return true;
    }

 

Link to comment
Share on other sites

Thanks.

I've tried it both your way and another way (see below). The tab gets added to the database but when I try to access the controller it still gives an error every time.

 

    public function install()
    {
        Configuration::updateValue('IKOSTOCK_LIVE_MODE', false);

        include(dirname(__FILE__).'/sql/install.php');

        return parent::install() &&
			$this->installTab() &&
			$this->registerHook('displayDashboardToolbarTopMenu');
    }

    private function installTab()
    {
        $tab = new Tab();
        $tab->class_name = 'AdminYourCustomController';
        $tab->module = $this->name;
        $tab->id_parent = (int) Tab::getIdFromClassName('AdminCatalog'); // Default tab, or use AdminCatalog
        $tab->active = 1;

        foreach (Language::getLanguages(true) as $lang) {
            $tab->name[$lang['id_lang']] = 'Custom Controller';
        }

        return $tab->add();
    }

 

Link to comment
Share on other sites

I just saw your update.

 

            $this->context->smarty->assign([
                'synchronize__url' => $this->context->link->getAdminLink('AdminYourCustomController', true, [], ['action' => 'synchronizeProducts'])  . '&ajax=true&id_lang=' . $this->lang,
            ]);

Like this, I no longer get the controller error, instead I get redirected to 

http://localhost:8081/admin985hli9ksojm6m5owdr/index.php?controller=AdminYourCustomController&action=synchronizeProducts&token=8c99384214334d3313e19be1bc689148&ajax=true&id_lang=1
 

and a page not found error.
 

Link to comment
Share on other sites

15 hours ago, Ikoshop said:

As I understand, you have working link now. Use it for ajax method in javascript.

Also create methods in your admin controller.

Link to comment
Share on other sites

No, I don't have a working link and I cannot get the controller to work. I've tried many other methods, with front and admin controllers, but none of the controllers are found when trying to access them from a direct URL in the format {sitename}/module/{modulename}/{controllername} or 

{sitename}/index.php?controller={AdminControllerName}

There must be something fundamentally wrong with my Prestashop installation as everything that is supposed to work does not when it comes to recognising the existence of controllers.

Link to comment
Share on other sites

Check that your controller is in yourmodule\controllers\admin folder

Name - AdminYourModule.php

And have code like this:

if (!defined('_PS_VERSION_')) {
    exit;
}

class AdminYourModuleController extends ModuleAdminController
{
    public function initContent()
    {
        parent::initContent();

....

	}
}

 

Link to comment
Share on other sites

Checked and rechecked lots of times.

I wondered if I needed a vendor/autoload.php file for it to work? I tried to make one with composer but when I load the page which accesses the module it throws the error 

 

Warning: include(/var/www/html/modules/mymodulename/vendor/composer/../composer/InstalledVersions.php): Failed to open stream: No such file or directory

Link to comment
Share on other sites

9 minutes ago, Ikoshop said:

I wondered if I needed a vendor/autoload.php file for it to work?

I don't use vendor folder.

So, when you enter this link: http://localhost:8081/admin985hli9ksojm6m5owdr/index.php?controller=AdminYourCustomController&action=synchronizeToPrestaShop&token=8c99384214334d3313e19be1bc689148&ajax=true&id_lang=1

You can see the Return values from your controller, right?

Link to comment
Share on other sites

20 hours ago, Ikoshop said:

Prestashop 8.2, trying to add a controller to a module.

File, called AdminYourCustomController.php in the controllers/admin directory of the module. Defined as 

class AdminYourCustomController extends ModuleAdminController{...}

called from my module

            $this->context->smarty->assign([
                'my_url' => $this->context->link->getAdminLink('AdminYourCustomController', true, [], ['action' => 'synchronizeProducts']),
            ]);

module uninstalled, reinstalled, cache cleared, read/write rights to the controller file checked and the controller is not recognised when called either by getAdminLink or a direct call to the URL of the controller.

I've seen on some old posts that a tab needed to be added and initialised in the install() method. I tried this but it didn't do anything.

Does anyone have a working solution for adding a controller in 8.2 ?

Make sure the controller is in the correct folder.
It should be as the following:  modules/your-module/src/Controller/AdminYourCustomController.php
It looks like you forgot the /src/ folder.

And maybe you could also share your namespacing and autloader config?

Edited by Inform-All (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...