Ikoshop Posted Thursday at 01:37 PM Share Posted Thursday at 01:37 PM 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 More sharing options...
webprog Posted Thursday at 02:50 PM Share Posted Thursday at 02:50 PM 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 More sharing options...
webprog Posted Thursday at 02:55 PM Share Posted Thursday at 02:55 PM Link in old form: $this->context->link->getAdminLink('AdminYourModuleName') . '&ajax=true&id_lang=' . $id_lang Link to comment Share on other sites More sharing options...
Ikoshop Posted Thursday at 02:57 PM Author Share Posted Thursday at 02:57 PM 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 More sharing options...
webprog Posted Thursday at 02:59 PM Share Posted Thursday at 02:59 PM Try to remove tab before new installation Link to comment Share on other sites More sharing options...
Ikoshop Posted Thursday at 03:05 PM Author Share Posted Thursday at 03:05 PM 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 More sharing options...
Ikoshop Posted Thursday at 03:14 PM Author Share Posted Thursday at 03:14 PM removing the tab from the dB and reinstalling doesn't change anything. I don't see a tab displayed anywhere on the product admin screen, despite it being in the database Link to comment Share on other sites More sharing options...
webprog Posted yesterday at 06:22 AM Share Posted yesterday at 06:22 AM 15 hours ago, Ikoshop said: 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. 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 More sharing options...
Ikoshop Posted yesterday at 08:12 AM Author Share Posted yesterday at 08:12 AM 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 More sharing options...
webprog Posted yesterday at 08:22 AM Share Posted yesterday at 08:22 AM 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 More sharing options...
Ikoshop Posted yesterday at 08:47 AM Author Share Posted yesterday at 08:47 AM 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 More sharing options...
webprog Posted yesterday at 09:00 AM Share Posted yesterday at 09:00 AM 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 More sharing options...
Ikoshop Posted yesterday at 09:02 AM Author Share Posted yesterday at 09:02 AM No, I just see 'page not found'. Link to comment Share on other sites More sharing options...
webprog Posted yesterday at 09:17 AM Share Posted yesterday at 09:17 AM 10 minutes ago, Ikoshop said: No, I just see 'page not found'. For proper functioning, the controller must return the return values. Try this link: http://localhost:8081/admin985hli9ksojm6m5owdr/index.php?controller=AdminYourCustom&token=8c99384214334d3313e19be1bc689148 without vars and corrected link to controller. Link to comment Share on other sites More sharing options...
Ikoshop Posted yesterday at 09:21 AM Author Share Posted yesterday at 09:21 AM Once again: page not found. I think I will have to abandon this approach and try find a solution which does not use a controller. Link to comment Share on other sites More sharing options...
Inform-All Posted yesterday at 10:13 AM Share Posted yesterday at 10:13 AM (edited) 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 yesterday at 10:15 AM by Inform-All (see edit history) 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