爱程序网

Getting started writing ZF2 modules

来源: 阅读:

FROM:http://mwop.net/blog/267-Getting-started-writing-ZF2-modules.html

 

Getting started writing ZF2 modules

During ZendCon this year, we released 2.0.0beta1 of Zend Framework. The key story in the release is the creation of a new MVC layer, and to sweeten the story, the addition of a modular application architecture.

"Modular? What's that mean?" For ZF2, "modular" means that your application is built of one or more "modules". In a lexicon agreed upon during our IRC meetings, a module is a collection of code and other files that solves a specific atomic problem of the application or website.

As an example, consider a typical corporate website in a technical arena. You might have:

  • A home page
  • Product and other marketing pages
  • Some forums
  • A corporate blog
  • A knowledge base/FAQ area
  • Contact forms

These can be divided into discrete modules:

  • A "pages" modules for the home page, product, and marketing pages
  • A "forum" module
  • A "blog" module
  • An "faq" or "kb" module
  • A "contact" module

Furthermore, if these are developed well and discretely, they can be re-used between different applications!

So, let's dive into ZF2 modules!

What is a module?

In ZF2, a module is simply a namespaced directory, with a single "Module" class under it; no more, and no less, is required.

So, as an example:

modules/    FooBlog/        Module.php    FooPages/        Module.php

The above shows two modules, "FooBlog" and "FooPages". The "Module.php" file under each contains a single "Module" class, namespaced per the module: FooBlogModule andFooPagesModule, respectively.

This is the one and only requirement of modules; you can structure them however you want from here. However, we do have a recommended directory structure:

modules/    SpinDoctor/        Module.php        configs/            module.config.php        public/            images/            css/                spin-doctor.css            js/                spin-doctor.js        src/            SpinDoctor/                Controller/                    SpinDoctorController.php                    DiscJockeyController.php                Form/                    Request.php        tests/            bootstrap.php            phpunit.xml            SpinDoctor/                Controller/                    SpinDoctorControllerTest.php                    DiscJockeyControllerTest.php

The important bits from above:

  • Configuration goes in a "configs" directory.
  • Public assets, such as javascript, CSS, and images, go in a "public" directory.
  • PHP source code goes in a "src" directory; code under that directory should follow PSR-0 standard structure.
  • Unit tests should go in a "tests" directory, which should also contain your PHPUnit configuration and bootstrapping.

Again, the above is simply a recommendation. Modules in that structure clearly dileneate the purpose of each subtree, allowing developers to easily introspect them.

The Module class

Now that we've discussed the minimum requirements for creating a module and its structure, let's discuss the minimum requirement: the Module class.

The module class, as noted previously, should exist in the module's namespace. Usually this will be equivalent to the module's directory name. Beyond that, however, there are no real requirements, other than the constructor should not require any arguments.

namespace FooBlog;class Module{}

相关文章列表: