Welcome Guest, Not a member yet? Register   Sign In
CI and MVC Best Practices (site structure, etc.)
#1

[eluser]MCrittenden[/eluser]
I just started using/learning CI about a week ago. I've found that there's a wealth of information about how to use it and how to get things done, but not much on the best way to do them.

I think a topic like this deserves a page in the user guide or something like that. It's easy for CI newcomers to get into bad habits just because they can't find anywhere to tell them how they should be doing things.

For example, here are a few questions I've come across so far.
- Where should js/css/image folders go? In the site root or somewhere in the application directory?
- Is it considered bad practice to put a function definition in a view, or sometimes acceptable, as Jim O'Halloran says?
- Is there an easy way to tell which functions belong in the model and which belong in the view?
- Is it always bad practice for a model to load a view?
- Are there any database/table/property naming conventions for CI that I should be following?
- What makes something a library as opposed to a helper or vise versa?

Any help would be appreciated. I am LOVING the what CI gives me, I just want to make sure I'm using it in the best way possible. Thanks guys.
#2

[eluser]gtech[/eluser]
- Where should js/css/image folders go? In the site root or somewhere in the application directory?
I put them in the root and then have a structure within each folder, other people may have a different approach. I don't think there is a right or wrong here.

- Is it considered bad practice to put a function definition in a view, or sometimes acceptable, as Jim O’Halloran says?
um javascript functions are fine in a view, I must admit I have not defined a php function in a view as everything I have done makes sense to put any function definitions in a library,helper,model or controller. I think you will find Jim O'Halloran says use functions like number_format which is a php function not a function you define.

- Is there an easy way to tell which functions belong in the model and which belong in the view?
Yes.. anything database orientated lives in the model do not put html'y bits in a model. You pass parameters to the model, the model does the database calls and then can optionally build the result data (eg an array of numbers) and return it.. a view should concentrate on displaying data that is passed to it by formatting it into some form of HTML document (or javscript, or some other format a browser understands).

- Is it always bad practice for a model to load a view?
yes, the model should just concentrate on database functionality like inserts,joins,updates etc and return the data back to the controller. I would seriously not load a view in a model.

- Are there any database/table/property naming conventions for CI that I should be following?
I created my own naming convention, I don't think CI imposes a ridged structure. However there are some expression engine developer guidelines you could follow, with a database section [url="http://expressionengine.com/docs/development/guidelines/general.html"]http://expressionengine.com/docs/development/guidelines/general.html[/url]

- What makes something a library as opposed to a helper or vise versa?
difficult decision sometimes to make.. a library I see more as complete set of functions to implement a task, where a helper is just a few functions that are useful in the development of more general tasks. sometimes you will get a cross over, and you need to make a decision.

the above is my own opinion, don't forget CI is supposed to be a loose frame work so find a strategy that suits you, but it does help obviously to keep to some consistent design.
#3

[eluser]MCrittenden[/eluser]
Thanks gtech. Your answers made a lot of sense.
#4

[eluser]gtech[/eluser]
ah good,

I had similar questions when starting off with CI, have a search around the forums when you are stuck as you will usually find someone has already asked most questions before.

welcome to CI!




Theme © iAndrew 2016 - Forum software by © MyBB