Welcome Guest, Not a member yet? Register   Sign In
Starting with CodeIgniter setup: suggestions & best practices
#23

[eluser]dmorin[/eluser]
Great list and I wanted to respond with some things that have helped me.
Quote:You’ll learn soon the value of putting the data-access in models, the logic in controllers and the visuals in views.

When I started, I put a lot of logic in my controllers which I found to be a huge mistake. I now try to follow these rules:
- If it's output, it should be in a view. However, you should try to keep as much logic as possible OUT of the views and instead put it in a helper (or if the formatting logic is more complex and deserves a class, continue down this list).
- If it involves any kind of data access (database, apis, etc), it should be in a model.
- If it's logic that could potentially be used in multiple applications (api access, generic classes), it should be a library.
- If it's logic that is application specific (some call this business logic) and it could potentially be used more than once in your application, it should be in a model.
- Only if the logic will, without a doubt, only be used once should you ever consider putting it in a controller since this is the part of CI that cannot be reused.

This kind of follows the DRY principal (Don't Repeat Yourself). Some will probably disagree which is ok, this is just my current best practice.

The only other thing I wanted to mention is related to your error reporting constant. I do a similar thing by setting a config item var called "is_production". If it's set to true (which I do at deploy time) it automatically turns off errors (including hiding errors in the default error views that CI ships with), turns on caching, and other things. That way, I can reference it anywhere in my app to make sure the production version runs with the correct options. You could also doing something like "app_state" and set it to dev/test/prod instead to have more granular control. That said, because it's in the config, it runs after CI initialization so I also disable errors in the index.php to make sure none are shown in case one happens before it processes the config.


Messages In This Thread
Starting with CodeIgniter setup: suggestions & best practices - by El Forum - 04-19-2010, 09:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB