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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!