4

I am a beginner with CodeIgniter still struggling to get a complete grasp on how to use the MVC ideology most cleanly.

I am writing a basic CMS system with the ability to vote on entries and follow people etc, consequently, I have found myself using the same or similar pieces of code across multiple views here and there consisting of various pieces of html and logic such as:

  • Voting panel
  • Follow/Unfollow panel
  • Login/Logout panel
  • Code to check if a user is logged in etc...

I am wondering where to put this code so it can be unified? I am thinking a helper is the way to go? If I declare the helper in the controller, it can be called from the corresponding view right?

Some of the elements are dynamic - such as a follow/unfollow button - It would need to check if you are already following the user or not and display the appropriate button, which would require a model to check. What I have now is that all the logic is in the controller and it returns an appropriate button, but it seems weird to be returning formed html code in a controller return as well. Should it be more like:

  • controller checks if you are following someone
  • the controller passes a boolean to the view
  • the view calls the helper with this value to draw the appropriate button

Also, as a secondary question, I have been doing a fair bit of looping through mysql arrays in foreach loops to process mysql results returned from the view. It seems like my views are getting somewhat complicated, but I can't think of another way to do it, although perhaps this should be done in another helper as well?

Apologies if this is a naive or repetitive question, there is indeed a lot of discussion surrounding this subject but it is not always easily relatable to another project.

tereško
  • 56,151
  • 24
  • 92
  • 147
waffl
  • 4,140
  • 5
  • 60
  • 108
  • Thanks for the answers so far - I am doing all of the DB fetching in the model, but am doing a fair bit of logic in the view. eg. I have the controller load an *entry* into a result, then all of the *votes* for each *entry* are loaded into another result. The view then has a nested foreach loop to display each entry and then the corresponding votes for that entry, is this 'okay' or should I be approaching it another way? @thrice801 - I'm just wondering why this user mentioned loading a helper in a view is not preferred - http://stackoverflow.com/questions/804399/codeigniter-create-new-helper – waffl Mar 22 '11 at 01:08

3 Answers3

0

Helpers are certainly one way to modularize anything that isn't DRY. Another is to use Partial Views. CodeIgniter looks like it supports partial views. Here's a good breakdown - not PHP specific but the discussion should be agnostic.

Community
  • 1
  • 1
JasonCoder
  • 1,106
  • 2
  • 13
  • 23
0

As far as handling user logins is concerned, you will probably want to use a static class and the singleton design pattern, which will allow you to check to see if a particular user is logged in or not anywhere in your application. There is a good tutorial here http://www.phpandstuff.com/articles/codeigniter-doctrine-scratch-day-4-user-login

Loading the helper, I don't believe loading it in your controller will automatically load it in your view. I think you have to re load the helper in your view file, or you have to autoload the helper. (cant remember off top of head but Im pretty sure).

Regarding looping through the mysql results, you should be using a model for this, always. Any functions which are grabbing or sorting information from your applicaiton, should be done within the model. Then, in your view file you loop through the results and format the data how you choose to.

thrice801
  • 1,616
  • 5
  • 22
  • 30
0

When developing http://newspapair.com which has the vote functionality you mentioned I used helpers and custom classes to spread the functionality across multiple views.

Helper - has functions without a class. So a standalone function or group of functions can be placed in a file and saved as a helper.

For instance I used a helper with generic form processing functions for NewsPapair, instead of a static class. But this is not the "best practices" thing to do. I did it this way because I already had the functions from a previous project.

As far a looping through MySQL results, try to write a query that allows the DB Server to do the heavy lifting. This will make your code more efficient. Perhaps ask a question about a specific query with example code. Plus do all of the data gathering in your Model.

Todd Moses
  • 10,777
  • 10
  • 44
  • 64