简体   繁体   中英

ZF2: Zend DB, TableGateway, Services Strategies

I have created a little Zend Framework 2 module called CMS to write simple articles in my site. This module follow uses Zend Db and the TableGateway class in order to get the data from the database.

I have read many website and books about the strategies to create a module and I prefer the short and fast way using these file structure:

.
├── Module.php
├── config
│   └── module.config.php
├── data
│   └── data.sql
├── src
│   └── Cms
│       ├── Controller
│       │   ├── IndexController.php
│       │   ├── PageAdminController.php
│       │   └── PageCategoryAdminController.php
│       ├── Form
│       │   ├── Element
│       │   │   ├── PageCategories.php
│       │   │   └── ParentPages.php
│       │   ├── PageCategoryFilter.php
│       │   ├── PageCategoryForm.php
│       │   ├── PageFilter.php
│       │   └── PageForm.php
│       ├── Hydrator
│       │   └── Strategy
│       │       └── DateTimeStrategy.php
│       ├── Model
│       │   ├── Page.php
│       │   ├── PageCategory.php
│       │   ├── PageCategoryTable.php
│       │   ├── PageTable.php
│       │   └── UrlRewrites.php
│       └── View
│           └── Helper
│               ├── Extract.php
│               └── Tags.php
└── view
    └── cms
        ├── index
        │   ├── index.phtml
        │   ├── notfound.phtml
        │   └── page.phtml
        ├── page-admin
        │   ├── edit.phtml
        │   └── index.phtml
        ├── page-category-admin
        │   ├── edit.phtml
        │   └── index.phtml
        └── partial
            └── tags.phtml

TableGateway Method

This file structure allows me to declare, for instance, the Page and PageTable class in the module.php and call the ServiceLocator to read and write the records from the database in this way:

$pageTable = $this->getServiceLocator()->get('PageTable');

In this case I can use this class to write the CRUD methods in the PageTable Class.


Inject Service Method

Then I have seen that there is a Service way to do the same thing where the CRUD actions are located in a Service Class that calls the TableGateway Class and inject by a Factory Class the Service into the __construct method of the Controller .

Service > TableGateway > Factory > Controller

Why have I to choose the Service strategy instead of the simple TableGateway?

Well your logic is a little off, the way is actually only

Controller calls Service calls TableGateway

The Factory is just a pattern to properly inject the dependencies.

Why use the Service

To abstract the behavior. The Service generally speaking is the instrument for your controllers to get the data. The Service then interacts with a data-source. What the data-source is, your controller doesn't care - hell not even your service should care. The service should only care for an implementation of an interface. This way, whenever you feel that you don't like TableGateway anymore but you wanna go Doctrine2, you don't have to change your Service. You don't have to change you Controller. All you have to change is the dependency of your Service. Instead of injecting a TableGateway class you inject your Doctrine2 class which matches the interface of the data-provider-dependency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM