33

opencart でカスタム管理パネル ページを作成する方法を知りたいです。

コントローラーでのログインが必要 - 管理パネルは通常のサイトと同じコントローラーを使用していないようです。opencart でカスタム ページを作成する方法を知っています(ただし、これは管理者向けではありません)。

シンプルな Hello World の例は素晴らしいでしょう

4

2 に答える 2

64

OpenCart 2.x

OpenCart2でパス名が変更されました-作成する必要があります

admin/controller/extension/module/hello.php admin/language/en-gb/extension/module/hello.php admin/view/template/extension/module/hello.tpl するとルートは

admin/index.php?route=extension/module/hello

OpenCart 1.x

  • 完全なMVCフローを含めます。

私はこれを行う方法を見つけました。OpenCartはMVCパターンを使用します。OpenCartの達人になる方法について読むことをお勧めしますか?システムがどのように機能するかについての投稿-この管理ワークフローは、顧客側にも十分です。

1)で新しいファイルを作成しますadmin/controller/custom/helloworld.php

ファイル名とコントローラー名は、説明の順序で同じである必要があります。

helloworld.php

<?

class ControllerCustomHelloWorld extends Controller{ 
    public function index(){
                // VARS
                $template="custom/hello.tpl"; // .tpl location and file
        $this->load->model('custom/hello');
        $this->template = ''.$template.'';
        $this->children = array(
            'common/header',
            'common/footer'
        );      
        $this->response->setOutput($this->render());
    }
}
?>

2)で新しいファイルを作成しますadmin/view/template/custom/hello.tpl

Hello.tpl

<?php echo $header; ?>
<div id="content">
<h1>HelloWorld</h1>
<?php
echo 'I can also run PHP too!'; 
?>
</div> 
<?php echo $footer; ?>

3)で新しいファイルを作成しますadmin/model/custom/hello.php

<?php
class ModelCustomHello extends Model {
    public function HellWorld() {
        $sql = "SELECT x FROM `" . DB_PREFIX . "y`)"; 
        $implode = array();
        $query = $this->db->query($sql);
        return $query->row['total'];    
    }       
}
?>

4)次に、許可拒否エラーを回避するためにプラグインを有効にする必要があります。

Opencart > Admin > Users > User Groups > Admin > Edit

アクセス許可を選択して有効にします。

あなたのページにアクセスするには、

www.yoursite.com/opencart/admin/index.php?route=custom/helloworld

于 2012-05-22T11:47:29.433 に答える
3

オープンカート 3.x

OC 1 および 2 と同じ MVC+L 構造を使用できます。詳細な例を次に示します。Lats はそれをCustom Pageと呼んでいます。


パスを使用してモデルを作成する/admin/model/custom/page.php

<?php
class ModelCustomPage extends Model {
    
    public function getTotalInformationsOnCustomPage() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "information");
        return $query->row['total'];
    }

}

ファイル パスとモデル名は同じである必要があります。model/custom/page.phpになりModelCustomPageます。

ここでメソッドを見ることができますpublic function getTotalInformationsOnCustomPage()。これは、たとえば、情報モデルから取得したものです。オプション。


パスを使用してコントローラーを作成する/admin/controller/custom/page.php

<?php
class ControllerCustomPage extends Controller {
    public function index() {
        
        $this->load->language('custom/page'); // calling Custom Page language

        $this->document->setTitle($this->language->get('heading_title')); // set title from Custom Page language

        $this->load->model('custom/page'); // calling Custom Page model
        
        $data['information_total'] = $this->model_custom_page->getTotalInformationsOnCustomPage(); // calling model method 
        
        // breadcrumbs
        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('custom/page', 'user_token=' . $this->session->data['user_token'], true)
        );
        
        // calling header, footer and column_left for our template to render properly
        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');
        
        $this->response->setOutput($this->load->view('custom/page', $data)); // send our $data array to view
    }
}

これは、すべてのデフォルト メニューとブレッドクラムを備えた、見栄えの良い管理ページの最小限のセットです。

モデルと同様に、ファイル パスとコントローラー名は同じである必要があります。controller/custom/page.phpになりControllerCustomPageます。


パスを使用して言語を作成する/admin/language/en-gb/custom/page.php

<?php
// Heading
$_['heading_title']           = 'Custom Page';

// Text
$_['text_custom_block']       = 'Custom Block';
$_['text_total_informations'] = 'Total informations:';

パスを使用してビューを作成する/admin/view/template/custom/page.twig

{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <h1>{{ heading_title }}</h1>
      <ul class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
        <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ul>
    </div>
  </div>
  <div class="container-fluid">    
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title"><i class="fa fa-thumbs-up"></i> {{ text_custom_block }}</h3>
      </div>
      <div class="panel-body">{{ text_total_informations }} {{ information_total }}</div>
    </div>
  </div>
</div>
{{ footer }}

この例では、このシステムに固有の標準ブロック構造を使用しました。任意の HTML を使用できます。ご覧のとおり{{ header }}、 、が標準の管理ナビゲーション サポート用{{ column_left }}に追加されました。{{ footer }}

ファイルを操作するときは、小枝のキャッシュをクリア.twigして変更を確認することを忘れないでください。


これらすべての操作の後、新しいアプリケーションのアクセス許可を設定することを忘れないでください。管理パネルでSystem > Users > User groupsに移動し、管理者グループ (またはその他の必要なグループ) を編集します。編集ページ セットで、アクセス許可ブロックと変更許可custom/pageブロックの両方を見つけて、選択済みとしてマークします。保存。


これで、新しいアプリケーションに URL yoursite.com/admin/index.php?route=custom/page&user_token=XXXXXXでアクセスできるようになりました

この時点から、カスタム ページを管理パネルの左側のメニューに追加することができます。コアファイルを編集するか、OCMOD ファイルを編集することでそれを行うことができます。

作成custom_page.ocmod.xml

<?xml version="1.0" encoding="utf-8"?>
<modification>
  <name>Custom Page OCMOD</name>
  <code>custom-page</code>
  <version>1.0</version>
  <author>Me</author>
  <link>http://mywebsite.com</link>

  <file path="admin/controller/common/column_left.php">
    <operation>
      <search><![CDATA[// Stats]]></search>
      <add position="before"><![CDATA[
      $data['menus'][] = array(
        'id'       => 'menu-custom',
        'icon'     => 'fa-thumbs-up', 
        'name'     => $this->language->get('text_custom'),
        'href'     => $this->url->link('custom/page', 'user_token=' . $this->session->data['user_token'], true)
      );
      ]]></add>
    </operation>
  </file>  
  
  <file path="admin/language/en-gb/common/column_left.php">
    <operation>
      <search><![CDATA[// Text]]></search>
      <add position="after"><![CDATA[
        $_['text_custom']                  = 'Custom Page';
      ]]></add>
    </operation>
  </file>  

</modification>

拡張機能 > インストーラーでファイルをインストールします。

Extensions > Extensionsに移動し、 OCMOD キャッシュをクリアします。

この OCMOD ファイルでは、2 つの OpenCart コア ファイルを直接編集せずに変更しただけです。これで、左側の管理メニューにカスタム ページへのリンクが表示されます。

OCMOD の詳細については、 Opencart Modification System Related Questions および OpenCart OCMOD and VQMOD Modification Systems を参照してください。

于 2020-08-26T16:59:33.083 に答える