Welcome Guest, Not a member yet? Register   Sign In
Controller / model separation, templating
#5

[eluser]dejan[/eluser]
[quote author="batfastad" date="1348296815"]
Code:
$this->load->database();
a DB connection is created, as I have autoinit set to TRUE in my config
[/quote]

You don't have to load database from the constructor all the time. You can just add it to application/config/autoload.php. Skip to the section "Auto-load Libraries" and make it look something like: $autoload['libraries'] = array('database');

[quote author="batfastad" date="1348296815"]
I have a constructor in each model which runs the above code and creates the connection.
Does this constructor only get fired when I call the first method in a model?
[/quote]

The constructor is called only when you first CREATE the object. It's not called ever again (and certainly not before method calls). E.g. take this code for an example:

Code:
class MyObject {
  public function __constructor($param) {
    echo "Constructor called with parameter $param";
  }

  public function method() {
    echo "Some method";
  }
}

$obj = new MyObject("foobar"); // This echos "Constructor called with parameter foobar". You can see the constructor was called, and that's it.
$obj->method(); // This echos "Some method". Constructor is never called again.

[quote author="batfastad" date="1348296815"]
6) Do I use a view for all output, e.g.: if I'm outputting JSON, XML or an ical/vcard?
Or are views just for HTML?
[/quote]

As you wish, really. For JSON, it doesn't make sense to call the view, because you can always do something like echo json_encode($stuff); and be done with it. You can do it in the view as well, but as it will usually end with that one line, no need for a view.

I personally only use views for HTML. However, there are people who prefer to create a view for e.g. XML and do something like

Code:
<element>&lt;?php echo $value; ?&gt;</element>

I prefer to construct XML using PHP DOM so I do everything in controllers and then just echo the output.


Messages In This Thread
Controller / model separation, templating - by El Forum - 09-21-2012, 11:00 AM
Controller / model separation, templating - by El Forum - 09-21-2012, 11:03 AM
Controller / model separation, templating - by El Forum - 09-21-2012, 03:47 PM
Controller / model separation, templating - by El Forum - 09-21-2012, 11:53 PM
Controller / model separation, templating - by El Forum - 09-22-2012, 12:41 AM
Controller / model separation, templating - by El Forum - 09-22-2012, 05:23 PM
Controller / model separation, templating - by El Forum - 09-23-2012, 11:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB