anon
anon

Reputation:

Implementing an "application logic" layer in CodeIgniter

In Asp.NET applications, there is a service layer that stands between the repositories and the controllers, so that different parts of the application can make use of the same logic, without any inconsistencies when one part changes.

For example, in CodeIgniter, if there is a part of the application that responds to REST API calls, I'd need to implement the same logic used in the Account controller for authentication. If there were something like UserService, that could be so easy.

What I'm asking is, do you think that this approach makes sense with CodeIgniter, if there is already a built functionality that covers that and if not, what would be the best way? Should I use libraries create my libraries which act like a service layer? Should I just put everything into models (probably not)? Or, wouldn't CodeIgniter be suited for apps which require that kind of complexity at all?

I need your insight on this. Thanks for the help.

Upvotes: 1

Views: 1778

Answers (2)

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

CodeIgniter can handle this perfectly. The idea is that the Controller is the main part of a request where logic is handled right? So a user hits a URL, they go to a controller and the controller decides wether to show them data, redirect them somewhere or tell them to shove off.

To share logic throughout your application you can create a MY_Controller (extending the main CI_Controller class) then this will be run for every controller. If you want to be more specific and have different kinds of controller loading different base controllers then you can.

http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

Upvotes: 4

coreyward
coreyward

Reputation: 80041

You can use a parent controller to stay DRY. PHP doesn't support any kind of mixins or multiple inheritance, though, so being able to add generic functionality to one or more of your controllers isn't quite as easy.

As far as authentication goes, allegedly this is one of the things that EllisLabs et al are working on for CodeIgniter 2.0 right now. They've mentioned that it's a pretty difficult task, and I think that is testament more to the fact that PHP sucks as a dynamic language than it does that CodeIgniter isn't suited to this sort of thing.

If you're not married to PHP/CI yet, thumbing through what Ruby on Rails offers might be of benefit. All this authentication is handled with mixins and metaprogramming in a really elegant and flexible way.

Upvotes: 1

Related Questions