Home > php教程 > php手册 > opencart分析

opencart分析

WBOY
Release: 2016-06-13 10:52:30
Original
985 people have browsed it

研究了两天opencart,对其中的实现原理做个小结,和各位共同探讨下:


opencart是自有开发的一套架构,其实现思想也是基于MVC的一种方式,架构的核心在system/engine下面,包括了几个文件,

    1):controller.php   所有控制器的基类

     2):action.php   动作转向,也就是路径,比如 catalog下面的类ControllerAccountAddress就是对于account/address

   3): front.php  前端动作执行文件,这个是在action.php基础上进行的操作,也就是相当于action.php是加载了数据,而front.php是动作,负责执行的。

   4):loader.php   这个是加载相关的类库文件,包括database下的,model下的,library下的文件,调用方法就是$this->load->library("library下的文件名“)

                                        其他参考loader.php文件,比如model,$this->load->model("account/address");

  5):model.php   这个文件是所有model的基类,不作多说明。

  6):registry.php  这个文件的实现和Model.php一样,这个类 Registry是整个系统的信息中枢, Registry是一个单例(Singleton),在index.php起始页面中,
     首先作为构造函数参数传 值 所要用到的类创建类实例,并随之将这个类实例设置到这个“注册表”中,
     这个注册表就像是一个共享的数据总线一样,把各个模块/数据串联在一起。

 

        在System下有一些公共类,所以的基础类和公共类都是通过index.php 去加载的,也就是 Registry注册的,这样你就可以去加载你需要的类和文件了

    var_dump($registry);exit;在index.php设置断点打印出来的内容 (截取部分)显示如下:

  

object(Registry)[1]
  private 'data' =>
    array
      'load' =>
        object(Loader)[2]
          protected 'registry' =>
            &object(Registry)[1]
      'config' =>
        object(Config)[3]
          private 'data' =>
            array
              ...
      'db' =>
        object(DB)[4]
          private 'driver' =>
            object(MySQL)[5]
              ...
      'url' =>
        object(Url)[8]
          private 'url' => null
          private 'ssl' => null
          private 'rewrite' =>
            array
              ...
      'log' =>
        object(Log)[9]
          private 'filename' => string 'error.txt' (length=9)
      'request' =>
        object(Request)[10]
          public 'get' =>
            array
              ...
          public 'post' =>
            array
              ...
          public 'cookie' =>
            array
              ...
          public 'files' =>
            array
              ...
          public 'server' =>
            array
              ...
          public 'request' =>
            array
              ...
      'response' =>
        object(Response)[11]
          private 'headers' =>
            array
print_r($registry->get('config')->get('account_module'));exit; 这是打印单个属性的内容

 

下面用实例来说明:

 

registry.php类的声明如下:

final class Registry {
private $data = array();
public function get($key) {
return (isset($this->data[$key]) ? $this->data[$key] : NULL);
}
public function set($key, $value) {
$this->data[$key] = $value;
}

public function has($key) {
     return isset($this->data[$key]);
   }
}

 

controller的声明如下(截取部分):

abstract class Controller {
protected $registry;
protected $id;
protected $layout;
protected $template;
protected $children = array();
protected $data = array();
protected $output;

public function __construct($registry) {
$this->registry = $registry;
}

public function __get($key) {
return $this->registry->get($key);
}

public function __set($key, $value) {
$this->registry->set($key, $value);
    }
}

 

 

任意声明一些变量:

$arr=array("mantis"=>"task","hefei"=>"anhui");
$str="中国安徽合肥";

声明一个类:

class  db{
   private $xx='123456';
   private $data=array();
public function get($key) {
    return (isset($this->data[$key]) ? $this->data[$key] : $key);
   }

   function connect(){
      echo 'you are connecting...';
   }
}

 

//声明一个控制类:

class ControllerAccountFix extends Controller{
 var $name;
 var $age;
 var $key='Opencat';
 function __construct(){
   $this->name='c';
   $this->age='12';
}
function fix(){
   echo $this->key.PHP_EOL;
 }
}

 

//声明注册类


$reg=new registry();

 

注册这些数据成为公共的部分:

        $reg->set("arr",$arr);
$reg->set("str",$str);
$reg->set("class",new ControllerAccountFix());
$reg->set("db",new db());


     $controller = new ControllerAccountFix($reg);
if (is_callable(array($controller, 'fix'))) {
$action = call_user_func_array(array($controller, 'fix'), array('dddd'));
}


//以上代码输出Opencart。


在把控制类重写一下:

class ControllerAccountFix extends Controller{
 protected $registry; www.2cto.com
 
function fix(){
 
   echo  $this->db->get('xx'); //输出123456

                  echo   $this->db->connect();//输出 you are connecting ...
 }
}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template