简体   繁体   English

初学者CodeIgniter概念 - 可重用的视图代码,去哪里? (帮手?)

[英]Beginner CodeIgniter concepts - Reusable view code, where to go? (Helper?)

I am a beginner with CodeIgniter still struggling to get a complete grasp on how to use the MVC ideology most cleanly. 我是CodeIgniter的初学者,仍然在努力完全掌握如何最干净地使用MVC意识形态。

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: 我正在编写一个基本的CMS系统,能够对条目进行投票并跟踪人员等,因此,我发现自己在多个视图中使用相同或类似的代码片段,其中包含各种html和逻辑,例如:

  • 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. 我现在拥有的是所有逻辑都在控制器中,它返回一个合适的按钮,但是在控制器返回中返回形成的html代码似乎很奇怪。 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. 另外,作为第二个问题,我已经在foreach循环中进行了一些循环mysql数组的处理,以处理从视图返回的mysql结果。 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. 抱歉,如果这是一个天真或重复的问题,确实有很多关于这个主题的讨论,但它并不总是很容易与另一个项目相关。

Helpers are certainly one way to modularize anything that isn't DRY . 帮手肯定是模块化任何非DRY的方法之一 Another is to use Partial Views. 另一种是使用部分视图。 CodeIgniter looks like it supports partial views. CodeIgniter 看起来像支持部分视图。 Here's a good breakdown - not PHP specific but the discussion should be agnostic. 这是一个很好的细分 - 不是PHP特定的,但讨论应该是不可知的。

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 这里有一个很好的教程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. 关于循环遍历mysql结果,你应该总是使用一个模型。 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. 然后,在视图文件中循环遍历结果并根据您的选择格式化数据。

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. 在开发具有您提到的投票功能的http://newspapair.com时 ,我使用了帮助程序和自定义类来跨多个视图传播功能。

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. 例如,我使用了具有NewsPapair的通用表单处理函数的帮助程序,而不是静态类。 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. 到MySQL循环结果为止,尝试编写一个允许DB Server完成繁重工作的查询。 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. 另外,您可以在模型中收集所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM