Welcome Guest, Not a member yet? Register   Sign In
best practice of exposing data to a view
#1

[eluser]Bacteria Man[/eluser]
I'm using a controller->library->model structure. The controller is extremely thin and is responsible for only displaying the view. The library contains all the business logic and the model interacts with the database.

With the above in mind, I'm trying to determine the best practice of exposing data to a view.

The most obvious way is to pass the data in the second parameter. For example:

Code:
$data['title'] = 'My Blog';
$this->load->view('blogview', $data);

What about data produced by the library? I like to return true/false from my functions. If false, the function failed for whatever reason. If true, the data is defined as an object and available to the view.

I suspect that a combination of both techniques is acceptable.

I'd appreciate any comments and/or a confirmation.

Thanks!
#2

[eluser]Michael Wales[/eluser]
Rather than return TRUE/FALSE from your models - why not return FALSE or any data?

That's the route I go - then, when passing data to the view (in the second parameter method your described) I determine whether everything is as it should be or if we have an issue.
Code:
<?
if ($myvar !== FALSE) {
  // do normal stuff
} else {
  // omg, die
} ?>
#3

[eluser]Bacteria Man[/eluser]
Thanks for your reply.

Yeah, you're right, that will work in most situations and returning the actual data (instead of true) makes perfect sense.

However, what about when the controller function is only concerned with success or failure and the data is to be used by a subsequent function? For instance, I have a library function that determines if the current user is the owner of a particular piece of data. If so, load the data into an object and return true. If false, alert the user that access is denied. This avoids having to hit the database twice: once to determine ownership and another to retrieve the data.

Does this fundamentally make sense?




Theme © iAndrew 2016 - Forum software by © MyBB