开发者

Code Igniter: returning data from DB, where to put this parsing/output logic

I'm returning a list of people from MySQL

So I have a model, a controller and a view.

As I'm outputting the list of people on a page, I need to check the length of the person's name and dynamically add a class to the that it's displayed in.

Before this was CI, I created a simple function called 'name_class' and put that in a common.php include. On the page, as I was outputting people's names, I'd pass the name to that and it would echo a class on the if the name was over a certain length.

In code igniter, where is the best place to put this function? I thought perhaps as a private function on the controller but then, can I access that from the view开发者_开发知识库?


It seems more logical to me that this belongs as a function within your view. If what your doing has something to do with how you are displaying the data, then it should be in the view layer of your code.

Though CodeIgniter is geared towards web applications, you should always consider the fact that the MVC design pattern is not specific to web pages/sites. The view layer of MVC is used to change how the data provided by the model is displayed or provided to the UI or client. You should attempt to put any client or UI based information or logic into the view and keep it hidden from the Model and Controller layers.

For example, it looks like your view is outputting the HTML directly which is consumed by the client side browser to display. Here is makes a lot of sense to output HTML, and provide the CSS classes needed based on the data provided by the Model. Now, consider what would need to change within your application if you decided to use a client library that accepts your data in JSON format and displays it. If your application's architecture is correct, you should only need to change the View to provide the information in JSON format and you shouldn't be leaving view specific code (i.e. your class logic) in another layer.


Not as a private function on the controller, but as a public function on the controller. If you created a function like this:

public function _test(){
    echo "HERE";
  }

then you could access it in the view with get_instance()->_test();. Make sure you begin the function name with an underscore to make sure it is not executable from the browser.

EDIT: you could also use a helper as documented at http://codeigniter.com/user_guide/general/helpers.html.


It definitely goes in the view, or if you can make the function useful enough to be used for more than just this one instance, put it in a helper - but still only call it in the view.

The view is your output. HTML belongs in the view whenever possible. class is HTML, so naturally you want to restrict it to your view.

Think of it like this (though this is a broad generalization):

  • Controller = Input
  • Model = Processing
  • View = Output

There is also no good way to access Controller functions directly from the view, you'd have to run it in your controller and pass it as data to the view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜