Beginner Concepts CodeIgniter - reusable view code, where to go? (Helper?)

I start with CodeIgniter, still struggling to figure out how best to use the MVC ideology.

I am writing a basic CMS system with the ability to vote on posts and keep track of people, etc., so I found that I use the same or similar code fragments in several views here and there, consisting of different parts of html and logic, for example:

  • Voting panel
  • Follow / Unfollow Panel
  • Entry / Exit Panel
  • Code to check if the user is registered, etc.

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

Some elements are dynamic - for example, the follow / unfollow button - you will need to check if you are already following the user or not, and display the corresponding button for which you need to check the model. Now I have that all the logic is in the controller, and it returns the corresponding button, but it seems strange to return the generated html code in the controller return. If it will be more like:

  • Controller
  • checks if you are following someone.
  • the controller passes the logical view to the view
  • the view calls a helper with this value to draw the corresponding button

Also, as a minor issue, I was doing a fair bit of the loop through mysql arrays in foreach loops to handle the mysql results returned from the view. It seems that my views are somewhat complicated, but I can not come up with another way to do this, although perhaps this should be done in another assistant?

Sorry, if this is a naive or recurring question, there really are a lot of discussions on this issue, but it is not always easy to relate to another project.

+4
source share
3 answers

Helpers, of course, are one way of modulating everything that is not DRY . Another is to use Partial Views. CodeIgniter looks like it supports partial views. Here is a good breakdown - not PHP, but the discussion should be agnostic.

0
source

Regarding the processing of user logins, you probably want to use the static class and singleton design template, which will allow you to check if any particular user is registered anywhere in the application. There is a good tutorial here http://www.phpandstuff.com/articles/codeigniter-doctrine-scratch-day-4-user-login

Downloading an assistant, I do not believe that downloading it in your controller will automatically load in your view. I think you need to reload the helper in your view file, or you need to autoload the helper. (I canโ€™t remember from my head, but Im pretty sure).

As for loops based on mysql results, you should always use a model for this. Any functions that capture or sort information from your application must be performed inside the model. Then, in your view file, you look at the results and format the data as you select.

0
source

When developing http://newspapair.com , which has a voting feature that you mentioned, I used helpers and custom classes to distribute functions in multiple views.

Helper - has functions without a class. Thus, a stand-alone function or group of functions can be placed in a file and saved as an assistant.

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

As the loops go through the MySQL results, try writing a query that allows the database server to perform a heavy lift. This will make your code more efficient. Perhaps ask a question about a specific request with sample code. In addition, all data is collected in your model.

0
source

Source: https://habr.com/ru/post/1344649/


All Articles