Welcome Guest, Not a member yet? Register   Sign In
A New Understanding of CI
#1

[eluser]Unknown[/eluser]
So I have been using code igniter for a couple of days... well no. i have been playing with it for a couple of days and now im going to start using it. I am developing a "Message Log" Application, and my config file contains things like the db info(host,uname,pass) as well as other information(company title, admin uname, other options).

Question: Whats the difference between a view and a model?
A view is a template of sorts and a model passes the info from the db to be put into the model.

Question: If i am correct above, then whats goes in the controller?
All of my information would be in the view/models, and then no reason for the controller.

Question: Where would i put my configs?
I believe they would go in the Controller and get passed to the model.

Question: Where would i put all of my resouces?
CSS,JS,Images,Flash. Normally i would have a /style and /js and /images folder at document root.
Question: Whats the best way to do Authentication with CI?
I want to use Sessions and cookies. I think i should use freakAuth from my reading. but how to i implement freakAuth? (or is that outside the realm of CI and i should go to freakAuth and find that out?)


So far im impressed with the things ive read about Code Igniter.
#2

[eluser]xwero[/eluser]
[quote author="brandenw" date="1193646811"]Question: Whats the difference between a view and a model?
A view is a template of sorts and a model passes the info from the db to be put into the model.[/quote]
View files are the only files that contain html, as a guide rule, so you can consider them as templates.
Models are classes where you make all the db queries, again as a guide rule.
CI doesn't force you to use this separation, it's possible you can put all the above code in a controller function or a library but to make your code reusable and easy to maintain it's best you follow the MVC pattern

[quote author="brandenw" date="1193646811"]Question: If i am correct above, then whats goes in the controller?
All of my information would be in the view/models, and then no reason for the controller.[/quote]
Using CI you can't exclude controllers because everything is routed to build pages through controllers. You have to look at a controller like a policeman regulating traffic. Users of your website want to go somewhere and the controller gives you the permission to go there. Sometimes to get some information you have to ask directions (forms) and then the controller shows you where to go.
To make it short the controller is the glue between the model and user actions and the view. You call the model methods in your controller to display that data in your view file.

[quote author="brandenw" date="1193646811"]Question: Where would i put my configs?
I believe they would go in the Controller and get passed to the model.[/quote]
If you have a lot of config values you can make an extra config file in the application/config directory and autoload it or you can set items in your controller. The last method has it's use if for instance there are many different configuration values per user or if you want to change the language of the site.
You can add the config values to the config.php file but then you have to check if there aren't changes in the config.php if you update to a newer CI version.

[quote author="brandenw" date="1193646811"]Question: Where would i put all of my resouces?
CSS,JS,Images,Flash. Normally i would have a /style and /js and /images folder at document root. [/quote]
you can do the same as you done before, even if you put CI in a non-public directory on your server.

[quote author="brandenw" date="1193646811"]Question: Whats the best way to do Authentication with CI?
I want to use Sessions and cookies. I think i should use freakAuth from my reading. but how to i implement freakAuth? (or is that outside the realm of CI and i should go to freakAuth and find that out?)[/quote]
There are several authentication libraries, you should check them to find the right one for you. Speaking for myself i find freakAuth a bit overwhelming and i'm not sure i would use all the features. But there is a good support for the library so any questions you have about it will be answered.
#3

[eluser]faceh[/eluser]
As a general rule of thumb (I agree with everything xwero said, but this is more condensed):

Controllers 'control' the data to be displayed
- ie. if's, else's, access checks, validation, etc.

Models are called by the Controller to grab database data.
- ie. anything involving SQL / mySQL / DB stuff goes in a model

Views simply display the data
- ie. they only do what they are told. No logic, if's or else's here


The following are my own opinions, which others may disagree with:

Libraries are used to process logic that may be used in more than one controller
- ie. Calculating a shopping basket total for display in different pages like basket, checkout and on the page sidebar

Helpers are like libraries but generally smaller and called by views
- ie. if you use the same layout in many views, rather than re-typing it every time, call it from a helper

Plugins - I have no idea Big Grin I would generally use libraries instead. Anyone offer a suggestion?
#4

[eluser]esra[/eluser]
[quote author="faceh" date="1195333824"]As a general rule of thumb (I agree with everything xwero said, but this is more condensed):[/quote]

I also agree wth xwero based on a CI point of view, but you need to realize that there are many implementations of MVC based on different development approaches. Rick Ellis generally takes a common sense point of view that is easily understood by anyone who is getting started with object-oriented programming. For anyone with experience working with alternate MVC implementations, CI is a bit of fresh air because Rick's development philosophy makes a great deal of sense. That is, Rick's approach is very intuitive to both the beginner and advanced developer.

[quote author="faceh" date="1195333824"]Controllers 'control' the data to be displayed
- ie. if's, else's, access checks, validation, etc.[/quote]

CI controllers are a bit passive. They load models, language files, plugins, helpers, and libraries, making those resources available to controller methods and the view. However, a CI controller does not mediate subcontrolers or a view.

[quote author="faceh" date="1195333824"]Models are called by the Controller to grab database data.
- ie. anything involving SQL / mySQL / DB stuff goes in a model[/quote]

Models do not necessarily have to load information from a database. For example, they could be used to handle the configuration file updates.

[quote author="faceh" date="1195333824"]Views simply display the data
- ie. they only do what they are told. No logic, if's or else's here[/quote]

Loader merely loads a file which is assumed to be a view based on the directory where the file is stored. However, it is fairly easy to subclass Loader.php to load templates from separate directories and partials (reusable views), from separate directories. For example, if you want to allow your users to switch between templates or allow a controller to load one of a selection of possible templates, it is possible to store those templates in a separate directory structure. Partials are view fragments. Some view fragments (parts of a template) are unique to a controller/view pair and are stored in views/. Some partials need to be reusable and loaded by multiple controllers. In a CMS like Postnuke or Xaraya, these are usually called blocks or modules in the case of Mambo/Joomla/Limbo. Blocks can be stored in a separate directory structure (e.g., under blocks). This makes it possible for the controller to load a selection of blocks.

[quote author="faceh" date="1195333824"]The following are my own opinions, which others may disagree with:

Libraries are used to process logic that may be used in more than one controller
- ie. Calculating a shopping basket total for display in different pages like basket, checkout and on the page sidebar[/quote]

It is possible to create a base controller from which all other controllers are inherited (extended). This base controller could include methods that are shared among multiple sibling (extended) controllers. However, it is wise to create libraries rather than a base controller when you need to selectively load classes with objects that are used by a subset of all controllers.

[quote author="faceh" date="1195333824"]Helpers are like libraries but generally smaller and called by views
- ie. if you use the same layout in many views, rather than re-typing it every time, call it from a helper[/quote]

True, but unless a helper is autoloaded or loaded by a specific controller, it is not available to the view. In the case or a reusuable partial (view fragment or block), the partial or block could use a helper to use controller-like code (functions) for performing controller-like operations or for accessing database. Actually, it is possible for a reusable partial (e.g., a block) to have a dedicated helper which provides controller-like functions.

[quote author="faceh" date="1195333824"]Plugins - I have no idea Big Grin I would generally use libraries instead. Anyone offer a suggestion? [/quote]

Plugins are pretty much under utilized in CI. They are a file like a helper. The major distinction between a helper and plugin is the directory from which they are loaded. When used in conjunction with hooks, plugins could be used to integrate third-party libraries and applications with CI. This topic probably needs better explanation in both the wiki and user guide, allowing developers to use plugins more often.

WHAT HAS NOT BEEN MENTIONED are configuration files and language files. By default, a config/ directory is provided under application/ for storing application/core specific configuration files. What many users do not realize is that custom configuration files could be created for individual controllers and those configuration variables could be loaded by the associated controller into the CI super object.

A language/ directory can optionally exist under application/. Controller-specific language files can be loaded by a controller and the language strings could be used as constants in a controller which could be reused in views and within controllers.
#5

[eluser]nate_02631[/eluser]
[quote author="faceh" date="1195333824"]Views simply display the data
- ie. they only do what they are told. No logic, if's or else's here[/quote]
Not quite true -- views will often have logic in them to loop through result sets, or display this or that conditionally...
[quote author="faceh" date="1195333824"]Libraries are used to process logic that may be used in more than one controller
- ie. Calculating a shopping basket total for display in different pages like basket, checkout and on the page sidebar[/quote]
I think something like the example above might be best put in the model... $this->cart->total Wink
#6

[eluser]esra[/eluser]
[quote author="nate_02631" date="1195369246"][quote author="faceh" date="1195333824"]Views simply display the data
- ie. they only do what they are told. No logic, if's or else's here[/quote]
Not quite true -- views will often have logic in them to loop through result sets, or display this or that conditionally...
[quote author="faceh" date="1195333824"]Libraries are used to process logic that may be used in more than one controller
- ie. Calculating a shopping basket total for display in different pages like basket, checkout and on the page sidebar[/quote]
I think something like the example above might be best put in the model... $this->cart->total Wink[/quote]

1) True, but a loop could also be handled by a helper function and assigned to a variable by a controller. Via variable replacement, the variable would be replaced by the loop. This is usually a better approach when working with a template designer with limited knowledge of PHP or for preferential reasons in order to minimize code in a view.

2) Also true, but there are always other means of handling this sort of thing. It never ceases to amaze me how this person or that person can develop alternate solutions for achieving the same result. I can honestly say that I learn at least one new useful approach every day by reading these forums.
#7

[eluser]nate_02631[/eluser]
@esra - yes, there are always "other ways" of doing things Wink ... the point of my prior post was to refute the somewhat inaccurate definitions of the poster above...

As to point #1, I don't work with template designers myself but any logic that is contained in the view would be the most remedial PHP there is, and any designer worth their salt would be able to pick it up in a matter of minutes. Personally, I find it a little sloppy to load up variables in the controller with HTML or other display stuff and strive for a pure separation of output in views.

That being said, the extent of php logic in my view is:
Code:
<p>&lt;?= $this == $that ? 'This is That' : 'This is NOT that' ?&gt;</p>

&lt;? if ($this == $that): ?&gt;
  <p>Conditional "blocks" used for displaying</p>
  <p>more than one line of HTML...</p>
&lt;? endif ?&gt;

<ul>
  &lt;? foreach ($items->result() as $item): ?&gt;
    <li>&lt;?= $item->name ?&gt;</li>
  &lt;? endforeach ?&gt;
</ul>
This has the advantages of keeping a strict separation of views (so you always know where to look for output), and provided your template designer is savvy, gives them *complete* control over the look of the page.




Theme © iAndrew 2016 - Forum software by © MyBB