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

Php 如何在opencart中创建自定义管理员页面?,php,e-commerce,opencart,Php,E Commerce,Opencart,我想知道如何在opencart中创建自定义管理面板页面 需要使用控制器登录-管理面板似乎没有使用与正常站点相同的控制器。我知道(但这不是给管理员的) 一个简单的Hello World示例非常好OpenCart 2.x OpenCart 2中的路径名已更改-您需要创建 admin/controller/extension/module/hello.php admin/language/en-gb/extension/module/hello.php admin/view/template/exte

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

需要使用控制器登录-管理面板似乎没有使用与正常站点相同的控制器。我知道(但这不是给管理员的)

一个简单的Hello World示例非常好

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模式。我建议阅读关于学习系统如何工作的帖子——这个管理工作流程对于客户端来说也应该足够了

1) 在
admin/controller/custom/helloworld.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'];    
    }       
}
?>
<?php
class ModelCustomPage extends Model {
    
    public function getTotalInformationsOnCustomPage() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "information");
        return $query->row['total'];
    }

}
<?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
    }
}
<?php
// Heading
$_['heading_title']           = 'Custom Page';

// Text
$_['text_custom_block']       = 'Custom Block';
$_['text_total_informations'] = 'Total informations:';
文件名和控制器名称的描述顺序应相同:

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

}
<?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
    }
}
<?php
// Heading
$_['heading_title']           = 'Custom Page';

// Text
$_['text_custom_block']       = 'Custom Block';
$_['text_total_informations'] = 'Total informations:';
选择并启用访问权限

要访问您的页面,请访问

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

opencart 3.x 我们可以使用与OC1和OC2相同的MVC+L结构。下面是一个详细的例子。Lats称之为自定义页面


使用路径
/admin/model/custom/page.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'];    
    }       
}
?>
<?php
class ModelCustomPage extends Model {
    
    public function getTotalInformationsOnCustomPage() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "information");
        return $query->row['total'];
    }

}
<?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
    }
}
<?php
// Heading
$_['heading_title']           = 'Custom Page';

// Text
$_['text_custom_block']       = 'Custom Block';
$_['text_total_informations'] = 'Total informations:';
这是漂亮的管理页面的最小设置,包含所有默认菜单和面包屑

和在模型中一样,您的文件路径和控制器名称应该相同
controller/custom/page.php
变成
ControllerCustomPage


使用路径
/admin/language/en-gb/custom/page.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'];    
    }       
}
?>
<?php
class ModelCustomPage extends Model {
    
    public function getTotalInformationsOnCustomPage() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "information");
        return $query->row['total'];
    }

}
<?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
    }
}
<?php
// Heading
$_['heading_title']           = 'Custom Page';

// Text
$_['text_custom_block']       = 'Custom Block';
$_['text_total_informations'] = 'Total informations:';
在这个例子中,我使用了这个系统固有的标准块结构。你可以使用任何你想要的HTML。如您所见,
{{{header}}
{{column_left}}
{{{footer}}
添加了标准管理员导航支持

使用
.twig
文件时不要忘记查看更改


在所有这些操作之后,不要忘记为新应用程序设置权限。在管理面板中,转到系统>用户>用户组,编辑管理员组(或/和您想要的任何其他组)。在“编辑页面集”中,在访问权限修改权限块中查找
自定义/页面
,并将其标记为选中。保存


现在,您的新应用程序可以通过urlyoursite.com/admin/index.php?route=custom/page&user\u token=XXXXXX进行访问

从这一点来看,您可能希望将自定义页面添加到管理面板的左侧菜单中。您可以通过编辑核心文件或更好地通过OCMOD文件来完成

创建
自定义页面.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>
扩展名>安装程序中安装文件

然后转到扩展>扩展

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

有关OCMOD的更多信息,请阅读 和

很好,重复-很好,啧啧。我认为这是不必要的,尽管
$this->load->model('catalog/information')
可能会减慢代码加载不需要的库的速度,尤其是在有多个管理员用户的繁忙站点上。@AlphaApp谢谢。对于评论和喜欢。admin/view/custom/hello.tpl应该由admin/view/template/custom/hello.tpl加载一个模型,即使有100名管理员查看同一页面,也不会有什么不同,除非这是一个非常糟糕的服务器出错,请建议出现致命错误:在第44行的C:\xampp\htdocs\bustersextra\system\engine\front.php中找不到类“Controllercustomhelloworld”