2016-05-30 119 views
1

我目前在將功能添加到Prestashop本地商店定位器的自定義模塊上工作。Prestashop 1.6 - 在自定義模塊中創建前端頁面

爲了我的需要,我必須在我的模塊(和第二個控制器)中創建第二個自定義頁面。

我嘗試了一些東西,但沒有任何工作。

我在目錄覆蓋FrontController文件 - > /module/override/controllers/front/StorepageController.php

<?php 

class StorepageController extends FrontController 
{ 

    public function initContent() 
    { 
     parent::initContent(); 

     $this->setTemplate(_PS_MODULE_DIR_.'modulename/views/templates/front/storepage.tpl'); 
    } 

,我把這個目錄中我.tpl文件 - > /模塊/視圖/模板/front/storepage.tpl

而結束,我抹去 「class_index.php」 我試試,看我的網頁與鏈接:?

本地主機/的Prestashop/FR/index.php的控制器= storepage

我不明白爲什麼沒有工作。

+0

當你說什麼都沒有工作,究竟發生了什麼? –

+1

如果我記得沒錯,你在前面的問題中覆蓋了默認的StoresController.php。這個覆蓋是否工作?在這裏,您正在嘗試創建第二個控制器,但您使用覆蓋語法。我將發佈一個模塊控制器創建的例子作爲答案。 –

+0

嗨, 當我嘗試訪問此自定義控制器時出現錯誤404頁面。 是的,這是另一個我想創建的控制器。 我的自定義StoresController完美工作。 在這裏,我想創建一個自定義的頁面,顯示來自我的StoresController的一些信息。 –

回答

2

這裏我們將在名爲CustomStores的模塊中創建一個名爲myStore的新控制器。我們會考慮到您已經忽略了默認的StoresController.php

你的模塊看起來是這樣的:

/customstores 
    /customstores.php 
    /config.xml 
    /override 
     /controllers 
      /front 
       StoresController.php 
    /views 
     /templates 
      /front 
       /stores.tpl 

現在,您希望有一個新的控制器,它不是一個替代。我們將通過擴展ModuleFrontController來創建這個新控制器。

你的模塊樹現在看起來就像這樣:

/customstores 
    /customstores.php 
    /config.xml 
    /override 
     /controllers 
      /front 
       StoresController.php 
    /views 
     /templates 
      /front 
       /stores.tpl 
       /mystore.tpl 
    /controllers 
     /front 
      /mystore.php 
    /classes 
     /CustomStore.php 

以下是mystore.php代碼:

<?php 

include_once(_PS_MODULE_DIR_ . 'customstores/classes/CustomStore.php'); 

/** 
* the name of the class should be [ModuleName][ControllerName]ModuleFrontController 
*/ 
class CustomStoresMyStoreModuleFrontController extends ModuleFrontController 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->context = Context::getContext(); 
     $this->ssl = false; 
    } 

    /** 
    * @see FrontController::initContent() 
    */ 
    public function initContent() 
    { 
     parent::initContent(); 

     // We will consider that your controller possesses a form and an ajax call 

     if (Tools::isSubmit('storeAjax')) 
     { 
      return $this->displayAjax(); 
     } 

     if (Tools::isSubmit('storeForm')) 
     { 
      return $this->processStoreForm(); 
     } 

     $this->getContent(); 
    } 

    public function getContent($assign = true) 
    { 
     $content = array('store' => null, 'errors' => $errors); 

     if (Tools::getValue('id_store') !== false) 
     { 
      $content['store'] = new CustomStore((int) Tools::getValue('id_store')); 

      if (! Validate::isLoadedObject($content['store'])) 
      { 
       $content['store'] = null; 
       $content['errors'][] = "Can't load the store"; 
      } 
     } 
     else 
     { 
      $content['errors'][] = "Not a valid id_store"; 
     } 


     if ($assign) 
     { 
      $this->context->smarty->assign($content); 
     } 
     else 
     { 
      return $content; 
     } 
    } 

    public function displayAjax() 
    { 
     return Tools::jsonEncode($this->getContent(false)); 
    } 

    public function processStoreForm() 
    { 
     // Here you get your values from the controller form 
     $like = Tools::getValue('like'); 
     $id_store = (int) Tools::getValue('id_store'); 
     // And process it... 
     $this->context->smarty->assign(array(
      'formResult' = CustomStore::like($id_store, $like) 
     )); 

     // And finally display the page 
     $this->getcontent(); 
    } 
} 

我還添加了一個自定義類你模塊調用CustomStore.php,在這裏我們去代碼:

<?php 

class CustomStore extends ObjectModel 
{ 
    public $id_custom_store; 

    public $name; 

    public $nb_likes; 

    /** 
    * @see ObjectModel::$definition 
    */ 
    public static $definition = array(
     'table' => 'customstores_store', 
     'primary' => 'id_custom_store', 
     'fields' => array(
      'name' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 128), 
      'nb_likes' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), 
     ), 
    ); 

    public static function like($id_store, $like) 
    { 
     $store = new CustomStore($id_store); 

     if (! Validate::isLoadedObject($store)) 
     { 
      return false; 
     } 

     if (! ($like == 1 || $like == -1)) 
     { 
      return false; 
     } 

     $store->nb_likes + (int) $like; 
     $store->save(); 
    } 
} 

您將不得不創建customstores_store表你的模塊install()方法內:

DB::getInstance()->execute(
    "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "customstores_store` (
     `id_custom_store` varchar(16) NOT NULL, 
     `name` varchar(128) NOT NULL, 
     `nb_likes` int(10) unsigned NOT NULL DEFAULT '0', 
     PRIMARY KEY (`id_custom_store`) 
    ) ENGINE=" . _MYSQL_ENGINE_ . " DEFAULT CHARSET=utf8;" 
); 

此代碼並沒有進行測試,並寫在一個單杆,但所有你需要的概念在這裏)。如果您想了解更多信息,我建議您查看其他核心模塊代碼。玩的開心 !

+0

幾分鐘前,我發現文檔讓我明白自己的錯誤。 而你確認這個錯誤。 你的CustomStore只是一個例子,說明如何在模塊控制器中調用和使用自定義類。再次感謝您的幫助! –

+1

是的。我應該補充一點,你可以使用這個URL訪問你的控制器:'http://mywebsite.com/index.php?fc = module&module = customstores&controller = mystore&id_store = 1'你可以在'SEO'部分的backoffice中定義一個友好的URL。 –

+0

我認爲我必須操縱和理解pretashop核心以更多設施進行編碼。 我開始同化,並感謝你的大力幫助。 –

相關問題