如何在opencart中创建自定义管理页面?

The*_*Kid 31 php e-commerce opencart

我想知道如何在opencart中创建自定义管理面板页面.

需要使用控制器登录 - 管理面板似乎不使用与普通站点相同的控制器.我知道如何使用opencart制作自定义页面(但这不适用于管理员)

一个简单的Hello World示例会很棒

The*_*Kid 62

OpenCart 2.x.

OpenCart 2中的路径名已更改 - 您将要创建

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

您的文件名和控制器名称应按desc顺序相同:

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());
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

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; ?>
Run Code Online (Sandbox Code Playgroud)

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'];    
    }       
}
?>
Run Code Online (Sandbox Code Playgroud)

4)然后,您需要启用该插件以避免权限被拒绝错误:

Opencart > Admin > Users > User Groups > Admin > Edit
Run Code Online (Sandbox Code Playgroud)

选择并启用访问权限.

要访问您的页面,请转到

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

  • 好,重复 - 很好的啧啧.我认为这不是必需的但是`$ this-> load-> model('catalog/information');`可能会减慢加载不需要的库的代码,特别是在拥有多个管理员用户的繁忙站点上. (2认同)
  • 更新了帖子. (2认同)
  • 即使有100位管理员查看同一页面,一个模型的加载也会有所不同,除非它是一个非常糟糕的服务器 (2认同)

foc*_*yle 5

OpenCart 3.x

我们可以使用与 OC 1 和 2 中相同的 MVC+L 结构。这是详细的示例。Las 称之为“自定义页面”


使用路径创建模型/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'];
    }

}
Run Code Online (Sandbox Code Playgroud)

您的文件路径和模型名称应该相同。model/custom/page.php变成ModelCustomPage.

这里你可以看到 method 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
    }
}
Run Code Online (Sandbox Code Playgroud)

这是美观管理页面的最小设置,包含所有默认菜单和面包屑。

与模型中一样,您的文件路径和控制器名称应该相同。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:';
Run Code Online (Sandbox Code Playgroud)

使用路径创建视图/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 }}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我使用了该系统固有的标准块结构。您可以使用任何您想要的 HTML。如您所见,添加了{{ header }}{{ column_left }}{{ footer }}来支持标准管理导航。

使用.twig文件时不要忘记清除树枝缓存以查看更改。


完成所有这些操作后,不要忘记为新应用程序设置权限。在管理面板中,转到系统 > 用户 > 用户组,编辑管理员组(或/和您想要的任何其他组)。在编辑页面集上找到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>
Run Code Online (Sandbox Code Playgroud)

在“扩展”>“安装程序”中安装文件

然后转到“扩展”>“扩展”清除 OCMOD 缓存

在这个OCMOD文件中,我们只是修改了2个OpenCart核心文件,没有直接编辑它们。现在您将在左侧管理菜单的自定义页面上看到一个链接。

有关 OCMOD 的更多信息,您可以阅读 Opencart 修改系统相关问题OpenCart OCMOD 和 VQMOD 修改系统