dr5779 2013-10-14 14:41
浏览 36

CodeIgniter项目结构

I have to create a php project using Codeigniter and Doctrine. I worked alot with j2ee and I would like to use the same project structure in my php project.

So here is what i'm thinking:

  1. Controllers eg(UserController)
  2. Services aka Models Interfaces (UserService)
  3. Services Implementantions eg (UserServiceImpl implements UserService)
  4. Dao Interfaces (UserDao)
  5. Dao Interfaces implmentations eg(DoctrineUserDao)
  6. Doctrine Entities
  7. Views

I haven t seen implemented in php projects interfaces for services and dao design pattern is always missing. Are Interfaces and DAO redundant in php mvc projects ?

And another question: as far as I know Codeigniter loads model using the following syntax: $this->load->model('UserServiceImpl'); which is kind of lame in my opinion, i prefer using autoloader with namespaces, is this bad ?

  • 写回答

1条回答 默认 最新

  • dpnfjx755573 2013-10-17 09:16
    关注

    I've designed a few smaller systems with CodeIgniter, and now I'm designing/building a big one. I always followed the same structure (the one I'm going to describe here) and it worked for me very well so far. For my current project we tried to use Doctrine as the ORM, but in the end I decided to leave it out from the project - it was more of a burden than help.

    (I may use slightly different terms for the layers, but I tried to put them in parallel with your terms wherever I could.)

    The structure I use is:

    1. Controllers (e.g. /application/controllers/UserController.php)
    2. Data Mapper (ORM) layer (e.g. /models/tables/UserTable.php)
    3. Domain Object layer (e.g. /models/data_models/User.php)
    4. Layouts (e.g. /models/layouts/default.php)
    5. Templates (views) (e.g. /application/templates/user/view-profile.php)

    Responsibilities:

    • (2) Data Mapper layer contains all the SQLs, and all Doctrine EntityManager usages. It stores and retrieves Domain Objects.
    • (3) Domain Objects represent the entities (with entity metadata described in comments for Doctrine, using the Docblock Annotations format).
    • (1) Controllers do only the logic of calling the ORM layer, maybe do some restructuring of data or calculations.
    • (4) The layout layer helps me a lot with separating the quasi-static frame of the pages from the more dynamic content. See CodeIgniter and layouts? if you like the idea.
    • (5) Templates are basically HTML with a few PHP snippets.

    All my files that contain classes contain one class per file, named the same as the filename (as per http://www.php-fig.org/psr/0/) but I don't use namespaces because I find it hard to make it work with CodeIgniter that doesn't use them.

    You can load your models in autoloader, especially if you work on a small or medium-sized project and performance is not critial. I always load all my models with the autoloader in these cases. However, on a bigger project it's more worthwhile to load widely-used models in the autoloader and more specific ones in the controller constructor or the more specific ones even in the actions.

    评论

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?