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

33

我想知道如何在opencart中制作自定义管理面板页面。

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

一个简单的 Hello World 示例将是很棒的。

2个回答

64

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中创建一个新文件。

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

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


2
好的,已点赞 - 很棒的教程。我认为这不是必要的,$this->load->model('catalog/information'); 可能会减慢您的代码加载速度,特别是在具有多个管理员用户的繁忙站点上加载不需要的库。 - AlphaApp
2
除非服务器真的很差,否则即使有100个管理员查看同一页,加载一个模型也不会有太大影响。 - Jay Gilford
1
请检查您在用户组中的权限,并确保tpl文件存在。 - TheBlackBenzKid
1
非常棒的教程。谢谢你。在最后一步:现在添加访问权限的路径似乎是:OpenCart管理 -> 用户 -> 用户组 -> 高级管理员 -> 编辑 -> 仅勾选访问权限或访问和修改权限并保存。 - pablofiumara
1
但是您需要提供令牌参数才能直接在管理区域访问,对吗? - Jithin
显示剩余14条评论

5

OpenCart 3.x

我们可以使用OC 1和2中相同的MVC+L结构。这里有一个详细的例子,让我们称其为自定义页面


使用路径/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 创建视图(view).

{{ 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文件时,请不要忘记清除twig缓存以查看更改。
所有这些操作之后,请不要忘记为您的新应用程序设置权限。在管理面板中,转到系统 > 用户 > 用户组,编辑管理员 组(或/和您想要的任何其他组)。在编辑页面中,在访问权限修改权限块中找到custom/page,并将其标记为已选。保存。
现在您的新应用程序可以通过URLyoursite.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 > Installer 中安装文件。

然后转到 Extensions > Extensions清除OCMOD缓存

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

有关OCMOD的更多信息,请阅读Opencart Modification System相关问题以及OpenCart OCMOD和VQMOD修改系统


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接