Home > Article > Backend Development > Some excellent practices of Codeigniter_PHP tutorial
Recently I am planning to take over and improve a project written by others using Codeigniter. Although I have used CI before, I wrote it completely according to my own wishes. Didn't follow some of CI's routines. For projects used by the public, it is best to follow the framework specifications, so it is better to summarize it to avoid making others laugh when they take over it in the future.
If you don’t know MVC yet, you should learn it as soon as possible. You will quickly realize the value of accessing data in the Model, performing business logic in the Controller, and writing HTML code in Views. You may wrinkle your forehead if you haven't programmed using this model before, but you should give yourself a chance to try it.
A rule of thumb is to put less stuff into the Controller and remember the DRY principle: don’t reinvent the wheel. When writing the same code in more than one place, you should try to write a library, helper, or model depending on its type. For example, the database connection class is used frequently, so it is made into a model (provided by the system).
Once you understand the essence of MVC, this will become a habit, and you will benefit a lot from MVC's concise code.
One principle is: leave complex operations to the Model. Controller is more like an architect. Model is hard work. View is the painter. The Controller only needs to throw things into the Model, and does not need to care whether the data is abnormal, and then returns a flag and corresponding data. In this way, the MVC architecture is reflected.
Model is actually like an electrical appliance such as a microwave oven. The simpler it is to use, the more people like it. (Put the food in - press start - ok, the rice is cooked.) The advantage of having fewer interfaces is that the Model upgrade code is optimized At that time, the coupling to the outside world was not high. Even if you write poorly internally, the interface is clean and easy to use.
It is best to place the system and application folders outside the webroot. If index.php is placed under the /public_html/ path of the FTP server, you should try to place the System under the root directory /system. In this case, only Your PHP files can be accessed through index.php.
Don’t forget to modify the values of $system_folder and $application_folder in the index.php file. The value of $system_folder should be relative to the index.php file, and the value of $application_folder should be relative to the system directory.
A common mistake is to forget to turn off PHP error and database error reporting, which is risky. In any public site, error_reporting should be set to 0, and can only be set to E_ERROR at most. The database setting db_debug should be set to false. Based on other security considerations, set error information not to be displayed ini_set('display_errors', 'Off');
As you code and debug, you should set error_reporting to E_ALL and address every note and warning before releasing your application.
A simple method is to set the value of db_debug to a constant MP_DB_DEBUG in the application/config/database.php file. When the website is running, set it as follows:
ini_set('display_errors', 'Off'); error_reporting(0); define('MP_DB_DEBUG', false);
In coding and debugging set to:
ini_set('display_errors', 'On'); error_reporting(E_ALL); define('MP_DB_DEBUG', true);
Before receiving any data to your program, whether it is POST data submitted by a form, COOKIE data, URI data, XML-RPC data, or data in the SERVER array, we recommend that you practice the following three steps:
Regarding SQL injection, XSS, and CSRF, you should first understand them before deciding whether to adopt methods to prevent them. You can refer to the security guidelines in the CI manual and the input and security categories. Perhaps the most important principle is to check all user input before submitting data to the database or file system.
CodeIgniter 有一个自带的库 Active Record 能够帮助你在不使用 SQL 语句的情况下写查询语句。这在你不太精通 SQL 语句或不知道怎样防止SQL注入的情况下是一个很好的方法。
当你需要更强大的工具时,你可以考虑使用 Object Relational Mapper ,就是鼎鼎大名的 ORM 了,遗憾的是,CodeIgniter 没有自带 ORM 库,不过也有一些其他很好的选择。
最流行的或许是 DataMapper OverZealous Edition (DMZ),还可以使用 Doctrine (这里有一个教程),另一个选择 RapidDataMapper 是作者自己的作品。
编写简洁的代码,并且理解你的代码,不要只是复制粘贴别人的代码,并且不断提高编码能力。手册上的开发规范是一个能学习怎样更好编写代码的地方。
1. DRY。不要总是重复造轮子,把能重用的代码放在它应该在的地方,比如libraries, helpers 或者是 models,而不是controllers,一个经验准则:当你复制代码的时候,也许你已经第二次把它放在了错误的地方。
2. Caching (缓存)。缓存是一个提高性能的很好的方式,尤其是减少数据库的访问。可以参考网页缓存和数据库缓存,或者在论坛上搜索其他的可选方案,比如 MP_Cache 是作者自己的作品。
3. HTTP headers (HTTP头部)。在客户端你能够通过单独发送HTTP头部使浏览器缓存页面来提高性能,当你使用 AJAX 的时候你也需要了解它来禁止浏览器缓存。
一个禁止缓存的例子:
$this->output->set_header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate"); $this->output->set_header("Cache-Control: post-check=0, pre-check=0", false); $this->output->set_header("Pragma: no-cache");
一个长时间保持缓存的例子(比如 css, javascript):
$this->output->set_header('Cache-Control: private, pre-check=0, post-check=0, max-age=2592000'); $this->output->set_header('Expires: ' . gmstrftime("%a, %d %b %Y %H:%M:%S GMT", time() + 2592000)); $this->output->set_header('Last-Modified: ' . gmstrftime("%a, %d %b %Y %H:%M:%S GMT", time() - 20));
在 MY_Controller 头部和 __construct 函数中添加以下内容,用于设定默认的模版信息,其中 SITE_NAME 需要自己在 application/config/constants.php 里面自己定义:
class MY_Controller extends CI_Controller { protected $_data; // 模版传值数组 protected $_tplext; // 默认模版后缀 protected $_header; // 默认头部模版 protected $_footer; // 默认底部模版 public function __construct () { parent::__construct(); $this->_data['title'] = SITE_NAME; $this->_tplext = '.php'; $this->_header = 'templates/header'; $this->_footer = 'templates/footer'; // 开发模式下开启性能分析 if (ENVIRONMENT === 'development') { $this->output->enable_profiler(TRUE); } } }
新增的控制器不再继承 CI_Controller,而改继承 MY_Controller:
class Index extends MY_Controller { public function __construct () { parent::__construct(); } /** * 前台首页 */ public function index () { $this->_data['title'] = '首页'; // 不指定则使用默认标题 SITE_NAME $this->_view('index/index'); } }
末了,再补充两个:
cache用以存储缓存文件,codeigniter文件夹包含了CI的基类CI_Base,为了兼容php4和php5,CI_Base有两个版本,其中php4版本的CI_Base继承于CI_Loader。libraries里存放了大部分常用的类库,最主要的三个类:Model,View和Cotronller,自己写的任何mvc都要继承于已有的mvc类;helpers里是一些函数(方法)集合,用以辅助其他模块的方便工作。language是一个语言包,用以支持多语言。
application文件夹用以存储您的应用程序,CI已经在内部为您增加了一些子文件,包括models、views、controllers、config、errors、hooks和libraries。其中前三个文件夹是用以创建模型、视图和控制器的。您的大部分工作都应该是创建属于自己的MVC,并可在config里加入配置文件,libraries里加入一些对象和方法,用来辅助您的模型和控制器工作。而hooks也是对CI_Hooks的扩展,具体内容见下面的章节。
当有一个http请求时,如http://www.google.com/blog/,首先进入CI的引导文件index.php。接下来我们看看index.php里做了哪些事情。
index first sets the application folder name to application and the system folder name to system. Then it makes a series of strict judgments and converts them into unix-style server absolute file paths. Specifically, it defines two The more important constant, APPPATH, is the folder path of the application. According to analysis, this path can be at the same level as system: htdocs/application/, or it can be placed in the system folder as its subfolder: htdocs/system/ application/, but it is recommended to use the second method, which looks neater; BASEPATH, the basic file path of the website document, is probably htdoc/system/; in the end, the index boot file is introduced into codeigniter/codeigniter.php. Next we take a look at what is done in codeigniter.
codeigniter.php introduces three files at the beginning: Common.php, Compat.php and config/constants.php. Common contains some functions, including load_class for loading class libraries and log_message for recording logs. , and show_404 that introduces error pages are several important functions; Compat mainly solves the function incompatibility problem in php4 and php5, while constants defines some constants for reading and writing file permissions.
Then codeigniter loads the first class library, Benchmark. One of the simplest applications of this class library is to calculate the time it takes from the beginning to the end of compilation of a web page, so you put a mark at the beginning of compilation. After the rendering is completed and a mark is added, the time spent can be calculated.
Then the second class library, Hooks, is loaded. Like Benchmark, this class library is under systemlibraries. The function of this class library is to provide you with an opportunity to perform other things before the program starts compiling. Hooks will Approximately 8 opportunities are provided for you to perform other tasks, see the User Guide for details. Here it imports the first hook.
Then load the Config, URI, Router, Output and other class libraries respectively. Then, check whether there is a cache_override hook. This hook allows you to schedule your own function to replace the _display_cache method of the Output class. If not, directly Call Output's _display_cache to check whether there is cache content. If there is, the cache will be output directly and exit; if not, the execution will continue.
After that, continue to load Input and Language. Note that the class libraries loaded before are all references; then there is another important load, which is the loading of the CI_Base object. First, the version of php will be judged. If it is php4 version, Loader will be loaded first, and then Base4, because CI_Base in Base4 inherits from CI_Loader, but in Base5, CI_Base and CI_Loader have no inheritance relationship.
The next step is also the really critical step. This step starts by loading a Controller class. This is an instance, not a reference; then the http address is parsed through the Router to get the names of the controller and methods, and then look at the application controllers Whether there is such a controller and method, if not, an error will be reported; if there is, the judgment will begin.
I will summarize this much first and will add more later.