Prestashop后台模块点击菜单标签出现404错误,需创建模板显示文本
Hey there! Let's sort out that 404 error and get your module's back office page displaying custom content. Here's a straightforward, step-by-step guide tailored to PrestaShop:
1. Fix the 404 Error: Register Your Back Office Controller & Menu Tab
The 404 almost always happens because your module hasn't properly registered a back office controller or the menu tab points to an invalid path. Let's fix that:
a. Create the Back Office Controller File
First, make a new directory controllers/admin/ inside your module folder. Then create a file named Admin[YourModuleName]Controller.php (replace [YourModuleName] with your actual module name, e.g., AdminMyCustomModuleController.php).
Here's the code for the controller—make sure to adjust the module name and class names to match yours:
class AdminMyCustomModuleController extends ModuleAdminController { public function __construct() { $this->bootstrap = true; // Enables PrestaShop's Bootstrap styling $this->table = 'configuration'; // Use an existing core table (safe if you don't have custom tables) $this->module = 'mycustommodule'; // Your module's folder name (all lowercase!) $this->lang = false; // Set to true if you need multi-language support parent::__construct(); // Remove default toolbar buttons (like Add/Edit) if you only want to display content $this->toolbar_btn = []; } // Override the render method to load your custom template public function renderList() { // Pass variables to your template $this->context->smarty->assign([ 'greeting_text' => 'Welcome to my module\'s back office page!', 'additional_info' => 'This is custom content pulled from the controller.' ]); // Load your template file return $this->context->smarty->fetch($this->getTemplatePath() . 'admin/module_dashboard.tpl'); } }
b. Register the Menu Tab During Module Installation
Open your module's main file (e.g., mycustommodule.php) and add code to install the menu tab that links to your new controller:
public function install() { // Call parent install + install our custom tab return parent::install() && $this->installBackOfficeTab(); } private function installBackOfficeTab() { $tab = new Tab(); $tab->class_name = 'AdminMyCustomModule'; // Matches your controller's class name $tab->module = $this->name; // Set parent tab (here we're placing it under the "Modules" parent menu) $tab->id_parent = (int)Tab::getIdFromClassName('AdminParentModulesSf'); // Set tab name for all active languages foreach (Language::getLanguages(true) as $lang) { $tab->name[$lang['id_lang']] = 'My Custom Module'; } return $tab->add(); } // Don't forget to uninstall the tab when the module is removed public function uninstall() { return parent::uninstall() && $this->uninstallBackOfficeTab(); } private function uninstallBackOfficeTab() { $tabId = (int)Tab::getIdFromClassName('AdminMyCustomModule'); if ($tabId) { $tab = new Tab($tabId); return $tab->delete(); } return true; }
2. Create Your Custom Template
Now make a template file to display your content. Create a directory views/templates/admin/ inside your module folder, then add a file named module_dashboard.tpl with this content:
<div class="panel"> <div class="panel-heading"> <i class="icon-list-ul"></i> {l s='My Module Dashboard' mod='mycustommodule'} </div> <div class="panel-body"> <h3>{$greeting_text}</h3> <p>{$additional_info}</p> <p>You can add any HTML/Smarty code here to customize the page!</p> </div> </div>
3. Final Checks to Fix 404
- Clear PrestaShop Cache: Go to Back Office > Advanced Parameters > Performance > Clear Cache, or delete the
var/cache/folder manually. - Verify Naming: Double-check that your controller filename, class name, and tab
class_nameall match exactly (PrestaShop is case-sensitive!). - Reinstall the Module: Uninstall and reinstall your module to ensure the tab and controller are registered properly.
Once you've done all this, click your module's menu tab—you should see your custom template with the text you added, no more 404!
内容的提问来源于stack exchange,提问作者Sanchi Garg




