Zend Framework 2的新增功能

When I started writing for SitePoint, one of my first articles was about Zend Framework. Since then, the framework has released version 2 stable. Apart from the name, version 2 is really a new project compared to the older version; ZF has been totally rewritten. In this article I’ll give you an overview of the new features and the changes that have been introduced.

当我开始为SitePoint撰写文章,我的第一篇文章是有关Zend Framework的。 从那时起,该框架发布了稳定的版本2。 除了名称之外,与旧版本相比,版本2实际上是一个新项目。 采埃孚已被完全改写。 在本文中,我将向您概述新功能和已引入的更改。

包装系统 (Packaging System)

You’ve up to four different ways to obtain ZF2. The first is the classic way, downloading one of the packages available from the website’s download page or from packages.zendframework.com. The second is similar, but you can download it from the ZF GitHub repository. The third is by using Composer, a tool for dependency management. To install ZF2 using Composer, add the following lines to your composer.json file:

您最多有四种获取ZF2的方法。 第一种是经典方式,从网站的下载页面packages.zendframework.com下载其中一个可用 软件包 。 第二个类似,但是您可以从ZF GitHub存储库下载它。 第三是通过使用Composer (一种用于依赖项管理的工具)。 要使用Composer安装ZF2,请将以下行添加到composer.json文件中:

"repositories": [
  {
    "type": "composer",
    "url": "https://packages.zendframework.com/"
  }
],

"require": {
  "zendframework/zend-config": "2.0.*",
  "zendframework/zend-http": "2.0.*"
}

The forth method is to use PEAR2_Pyrus, running these commands:

第四种方法是使用PEAR2_Pyrus,运行以下命令:

pyrus.phar .
pyrus.phar . channel-discover packages.zendframework.com
pyrus.phar . install zf2/Zend_Framework#Standard

For version 2, ZF has developed and released a base skeleton application to get you started, with which you can start developing your own. You can download it from GitHub or using Composer:

对于版本2,ZF已开发并发布了基本的骨架应用程序以帮助您入门,并可以使用它开始开发自己的应用程序。 您可以从GitHub或使用Composer下载它:

composer.phar create-project --repository-url="http://packages.zendframework.com" zendframework/skeleton-application path/to/install

自动加载系统 (Autoloading System)

ZF2 seems to provides you with a lot of different alternatives for every new feature. The autoloading system, for example, now has three different options. You’ll see that there’s no trace of the require_once lines that were heavily used in the first version. Of course, a single require_once is needed to enable the new autoloading system, the one for the autoloader. The code to use should resemble the following:

ZF2似乎为每个新功能提供了许多不同的替代方法。 例如,自动加载系统现在具有三个不同的选项。 您会看到在第一个版本中没有大量使用的require_once行的痕迹。 当然,只需要一个require_once即可启用新的自动加载系统,即自动加载器系统。 使用的代码应类似于以下内容:

<?php
require_once 'Zend/Loader/StandardAutoloader.php';
$autoLoader = new ZendLoaderStandardAutoloader(array(
    'fallback_autoloader' => true,
));
$autoLoader->register();

This method loads namespaced classes using the PSR-0 standard (a great discussion on PSR-0 has been written by Hari K T in his article entitled Autoloading in PHP and the PSR-0 Standard). Note that setting 'fallback_autoloader' => true enables a fallback that allows you to invoke the PHP default inclusion system to include those files that don’t fall in the Zend namespace.

此方法使用PSR-0标准加载命名空间类(Hari KT在其题为PHP的自动加载和PSR-0标准的文章中对PSR-0进行了深入讨论)。 请注意,设置'fallback_autoloader' => true启用一个后备功能,该后备功能允许您调用PHP默认包含系统以包含那些不在Zend名称空间中的文件。

The second method is based on a class map, a file with an associative array where the keys are the class names and the values are their absolute path. Being that the class map is a simple file that returns absolute paths, this method can be faster compared to the previous one, up to 20% in its standard usage or up to 80% using an opcode cache system like APC. What’s tedious in this system is the creation of the map file itself, especially for larger projects. Luckily the ZF team created a class map generator that can automatically build the map for you. By default it’ll search for files into the current directory and write the output in a file created in the specified path. An example of its use is:

第二种方法基于类映射,该类映射是具有关联数组的文件,其中键是类名,值是它们的绝对路径。 由于类映射是一个返回绝对路径的简单文件,因此与以前的方法相比,此方法可以更快,其标准用法最多可以达到20%,而使用APC之类的操作码缓存系统则可以达到80%。 该系统中繁琐的工作是创建地图文件本身,特别是对于较大的项目。 幸运的是,采埃孚(ZF)团队创建了一个类地图生成器 ,可以自动为您构建地图。 默认情况下,它将在当前目录中搜索文件,并将输出写入在指定路径中创建的文件中。 其用法的一个示例是:

php classmap_generator.php My/Project

However, you can configure the script. For an overview of the options available, take a look at the class map generator documentation.

但是,您可以配置脚本。 有关可用选项的概述,请参阅类映射生成器文档

The third method is quite similar to the first. What changes in this case is that you can specify the path of the files using a given namespace like the examples below.

第三种方法与第一种方法非常相似。 这种情况下的变化是,您可以使用给定的名称空间指定文件的路径,如以下示例所示。

<?php
require_once 'Zend/Loader/StandardAutoloader.php';
$autoLoader = new ZendLoaderStandardAutoloader();
$autoLoader->registerNamespace('ApplicationNamespace', 'Path/to/files');
$autoLoader->register();

Or, you can set a specific prefix.

或者,您可以设置特定的前缀。

<?php
require_once 'Zend/Loader/StandardAutoloader.php';
$autoLoader = new ZendLoaderStandardAutoloader();
$autoLoader->registerNamespace('FilesPrefix_', 'Path/to/files');
$autoLoader->register();

依赖系统 (Dependencies System)

The developers tried to write code so it was as decoupled as possible and, to achieve this goal, the new version has been developed following the Inversion of Control (IoC). As Wikipedia states, IoC is “a programming technique, expressed in terms of object-oriented programming, in which object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis.” For a good explanation on this pattern, I suggest you take a look at Alejandro Gervasio’s article Inversion of Control – The Hollywood Principle.

开发人员试图编写代码,以使其尽可能分离,并且为了实现此目标,在控制反转(IoC)之后开发了新版本。 如Wikipedia所述,IoC是“一种以面向对象的编程方式表达的编程技术,其中对象耦合在运行时由汇编程序对象绑定,并且通常在使用静态分析时在编译时不知道”。 为了对此模式有一个很好的解释,我建议您看一下亚历杭德罗·格瓦西奥(Alejandro Gervasio)的文章“控制反转-好莱坞原理”

事件管理器 (Event Manager)

The last feature I’d like to discuss in this article is the new Event Manager implemented by the class ZendServiceManager that replace the component ZendApplication of the previous 1.x version. ZF2 is, in fact, now event-driven. As you might already know, before actually running the controller’s action, the framework does a lot of things. The first is to run the bootstrap which sets up the app modules and configuration. After the bootstrap, the URL requested by the user is parsed to route the application in the right way (usually module/controller/action) and then starts the dispatcher. In each of these steps you have a set of events that you can manage in almost any way you like to change the application flow.

我在本文中要讨论的最后一个功能是由类ZendServiceManager实现的新事件管理器,该事件管理器替换了先前1.x版本的组件ZendApplication 。 实际上,ZF2现在是事件驱动的。 您可能已经知道,在实际运行控制器的动作之前,该框架会做很多事情。 首先是运行引导程序,以设置应用程序模块和配置。 引导后,将解析用户请求的URL,以正确的方式(通常是模块/控制器/操作)路由应用程序,然后启动调度程序。 在每个步骤中,您都有一组事件,可以用几乎想要更改应用程序流的任何方式对其进行管理。

You can add events through the attach() method of the EventManager class. It accepts the name of the event to listen for, a callback function to call when the event is fired, and an (optional) parameter that specifies the event’s priority. The latter is specified by a positive integer that by default is 1. The higher the number, the higher the priority and earlier the execution.

您可以通过EventManager类的attach()方法添加事件。 它接受要侦听的事件的名称,在事件触发时要调用的回调函数以及指定事件优先级的(可选)参数。 后者由一个默认为1的正整数指定。数字越大,优先级越高,执行越早。

The event can be called using the trigger() method that also belongs to EventManager. Its parameters are the name of the event to fire, the context which is usually an instance of the object who has the event (or null if you use an anonymous function), and an array containing parameters to pass to the event handler. A simple example is the following:

可以使用也属于EventManagertrigger()方法调用该事件。 它的参数是要触发的事件的名称,上下文(通常是具有该事件的对象的实例)(如果使用匿名函数,则为null)以及包含要传递给事件处理程序的参数的数组。 一个简单的例子如下:

<?php
// Attach the handler
$event = new EventManager();
$event->attach(
   'myEvent',
   function($event) {
      $parameters = $event->getParams();
      echo 'The given name is ' . $parameters['name'] . ' ' . $parameters['surname'];
   },
   100
);

// Fire the event
$parameters = array('name' => 'Aurelio', 'surname' => 'De Rosa');
$event->trigger('myEvent', null, $parameters);

结论 (Conclusions)

Throughout this article you learned about the major changes in Zend Framework 2. To sum things up, these are the key points I discussed:

在本文中,您了解了Zend Framework 2的主要变化。总而言之,这些是我讨论的关键点:

  • New packaging system, based on composer and pyrus, to download and install

    基于作曲家和pyrus的新包装系统,可以下载并安装
  • New MVC architecture based on events

    基于事件的新MVC架构
  • Better performance

    更好的性能
  • New autoloading class system

    新的自动加载课程系统
  • ZF2 uses several new design patterns like Event Manager and Dependency Injection that help you to decouple your code

    ZF2使用事件管理器和依赖注入等几种新的设计模式来帮助您分离代码

You may be overwhelmed by all these changes, and I think this is not so strange due the wide range of new features and improvements that have been introduced. In fact, this article just skims the surface. Feel free to download a copy of the framework and explore.

所有这些更改可能会让您不知所措,并且我认为这并不奇怪,因为已经引入了许多新功能和改进。 实际上,本文只是表面介绍。 随意下载该框架的副本并进行探索。

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/whats-new-in-zend-framework-2/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值