Prestashop Module with controller throws 404

2019-07-27 07:12发布

问题:

I am developing a module with a controller which is intended to read id_cart and do some actions. But I cannot invoke Controller, it always returns 404 error.

Module:

<?php
if (!defined('_PS_VERSION_'))
  exit;

class CartPortkey extends Module
{
  public function __construct()
  {
    $this->name = 'cartportkey';
    $this->tab = 'checkout';
    $this->version = '1.0.0';
    $this->author = 'Me and nobody else';
    $this->need_instance = 0;
    $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); 
    $this->bootstrap = true;

    parent::__construct();

    $this->displayName = $this->l('My Module Name');
    $this->description = $this->l('My Module Description.');

    $this->confirmUninstall = $this->l('Estás seguro de desinstalar?');
  }
}

Controller

<?php

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

class CartPortkeyFrontController extends ModuleFrontController   {
    public function init(){
        parent::init();
        $id_cart = (int)Tools::getValue('id_cart');
        $this->context->cookie->id_cart = $id_cart;
        $link_order = $this->context->link->getPageLink('order');
        Tools::redirect($link_order);
    }
    public function initContent() {
        parent::initContent();
    }

}

?>

I am trying this url: http://localhost/shop/myshop1/index.php?fc=module&module=cartportkey&controller=cartportkeyfrontcontroller&id_cart=2

I have to specify that I have enabled multistore where shop is the main and myshop1 is one of the 3 shops.

Folder Structure:

+ cartportkey
-- +controllers
-- -- +front
-- -- -- CartPortKeyController.php
-- cartportkey.php

I have ensured that the module is installed and active in all the stores.

回答1:

You have the controller naming convention wrong.

You need to declare the front controller class as follows.

ModuleNameControllerFileNameModuleFrontController extends ModuleFrontController

So currently your controller class should be declared as

CartPortKeyCartPoortKeyControllerModuleFrontController extends ModuleFrontController

Then load the controller with the following url

http://localhost/shop/myshop1/index.php?fc=module&module=cartportkey&controller=cartpoortkeycontroller&id_cart=2