PHP: MVC framework mode and Composer

PHP: MVC framework mode and Composer

Collaborative start today's MVC has been widely consensus, just as peace and development are the themes of the 21st century, before the code is the difference between the layer and the layer is not obvious, html and PHP mixed-use development, the efficiency is not high, in 1999, sun's J2EE, is the earliest and most complete MVC model.

advantage:

  • More view shared a Model. View in charge is responsible for formatting and presenting them to the user, business logic and presentation layers were separated, can be reused with a different Model View, greatly improves the reusability of code.
  • Controller is a high cohesive independent objects, remain independent and Model View.
  • Controller enhances the flexibility and configurability of the application.

MVC model emphasizes the separation of duties, brings important features of a modern software engineering requirements: testability, based on the program under good MVC separation of duties design, each part can be independently tested unit, which will help automate testing, integration, and many other advantages.

Disadvantages:

  • MVC will generate a lot of paper, for the PHP language that can not be permanent memory, a large number of files and load variables caused a lot of overhead.
  • Beginner programmers is not easy to understand.

Composer, to achieve and the introduction of third-party libraries, and its implementation relies on the use of management and automatic upgrades.

Composer solve the problem:

  • Do you have a library of more than rely project N
  • These libraries are also dependent on a number of projects in other libraries
  • You declare you rely on libraries
  • Composer identify those packets that version will be installed

Installation and Use

//安装命令
curl -sS https://getcomposer.org/installer | php

//设置全局变量
mv composer.phar /usr/local/bin/composer

Composer commonly used commands:

// 从当前目录读取composer.json文件,处理了依赖关系,并把其安装到vender目录下,如果当前目录下,如果当前存在composer.lock文件,他会从此文件读取依赖版本
composer install 

//获取依赖的最新版本,并且升级composer.lock文件
composer update

//打印自动加载索引,某些情况下你需要更新autoloader,例如在包中加入了一个新类。
composer dump-autoload

Loading:

  • PSR-0

  • PSR-4

  • class-map

  • Direct file contains

      {
          "autoload": {
              "psr-4": {
                  "Monolog\\": "src/",
                  "Vendor\\Namespace\\": ""
              }
          }
      }
    

composer load Principle: Key and value to define the namespace and its corresponding directory mapping. When you try to automatically record Vendor\\Namespace\\the use of the class, we will look for src/the file, if it exists automatically loaded, should be noted that this case Vendor\\Namespace\\does not appear in the file path.

And composer.json such a configuration will be converted into Composer namespace file directory MAP form and the presence vender / composer / autoload_psr4.php file. So if you install an extension is automatically loaded PSR-4 form, you can find his true path in autoload_psr4.php.

Composer advantages:

  • More centralized management \ automatically load library
  • Unified management of third-party libraries, support for scalable expansion

Jane says CI framework

The CI advantages:

Single entry: that each request will need to go through the entrance of this document processing, thereby performing file management Controller layer of a breeze, developers can implement unified management for many layer even View Controller layer in this entry, such as character set set, CSRF filtration.

MVC toward lightweight, API development of the (main JavaScript + Json form)

Published 98 original articles · won praise 185 · views 90000 +

Guess you like

Origin blog.csdn.net/xuezhiwu001/article/details/102847343