Catalin
Catalin

Reputation: 762

CodeIgniter project structure

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 ?

Upvotes: 3

Views: 1058

Answers (1)

David Veszelovszki
David Veszelovszki

Reputation: 2772

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.

Upvotes: 1

Related Questions