2016-05-30 12 views
1

私は現在、Prestashopネイティブストアロケータに機能を追加するカスタムモジュールで動作します。Prestashop 1.6 - カスタムモジュールでフロントエンドページを作成

自分のニーズに合わせて、自分のモジュール(および2番目のコントローラ)に2番目のカスタムページを作成する必要があります。

私は何かを試しましたが、何も動作しません。

私は自分のディレクトリに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を" 消去し、私は、このリンクで自分のページを見てみてください:

はlocalhost/PrestaShopの/ FR/index.phpをコントローラ= storepage

何も問題がない理由は分かりません。

+0

何も問題がないと言うと、何が起こっているのですか? –

+1

私が覚えていれば、前の質問でデフォルトのStoresController.phpを上書きしました。このオーバーライドは機能していますか?ここでは、2番目のコントローラを作成しようとしていますが、オーバーライド構文を使用しています。モジュールコントローラ作成の例を回答として投稿します。 –

+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を拡張してこの新しいコントローラを作成します。

あなたのモジュール・ツリーは次のようになります:私達はのために行くここで、

<?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と呼ばれるモジュール用のカスタムクラスを追加しました:

/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 

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(); 
    } 
} 

あなたのモジュール内の表は、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

はい、そうです。 'http://mywebsite.com/index.php?fc = module&module = customstores&controller = mystore&id_store = 1'を使用してコントローラにアクセスできることを付け加えておきます。' SEO'セクションでフレンドリーなURLをバックオフィスに定義することができます。 –

+0

私は、より多くの施設でコード化するためにプリタッシュコアを操作し理解する必要があると思います。 私は同化し、あなたの大きな助けに感謝します。 –

関連する問題