Getting started with CODEIGNITER development framework in PHP (1/4)

Source: Internet
Author: User
Tags autoload constructor php file php and regular expression reserved codeigniter

CodeIgniter URL:
Www.111cn.net/class/function/ID
1. The first section indicates calling the controller class.
2. The second part indicates a function or method in the call class. (If your class file is saved in a subdirectory, the second segment contains two segments: subdirectory/class function)
3. The third and more segments represent the parameters passed to the controller, such as ID or other variables.
(The first, second, and third sections refer to the segments separated by the slash "/" except the domain name and index. php)
By setting the $ config ['url _ suffix '] parameter in the application/config. php file, you can add a specified file suffix to the url generated by CodeIgniter.
For example: www.111cn.net/index. php/products/view/shoes
You can add a suffix, such as. html, to display it:
Www.111cn.net/index. php/products/view/shoes.html
You can enable the query string (such as index. php? C = products & m = view & id = 345). Note: If you use a query string, you must use your own URL, in addition, you cannot use URL helper functions (or other helper functions that generate URLs, such as form helper functions), because these are designed based on segmented URLs.
Custom URL route routing rules:
1. route rule file location: application/config/routes. php = "$ route array
2. Routing rules (wildcard rules and regular expression rules ):
A typical wildcard routing looks like this:
$ Route ['product/: num'] = "catalog/product_lookup"; // The array key contains the matched URI, the value of the array contains the destination to which the route will be redirected.
The key of the route array can match the text value or use the following two wildcard types:
: Num will match a segment that only contains numbers.
: Any will match a segment containing any characters.
You can also use a regular expression, for example, $ route ['products/([a-z] +)/(\ d +) '] = "$1/id _ $2 & Prime ;;
Wildcards and regular expressions can be used in combination.
3. Routes to be retained: $ route ['default _ controller'] and $ route ['scaffolding _ trigger']
Controller)
Create a controller:
1. Storage location of controller files: application/controllers/
2. Controller class File name: use the controller class name in full lowercase format as the file name, and use. php as the extension
3. Controller class declaration format: (1) must be a subclass of the Controller class; (2) the first letter of the class name must be capitalized.
For example, the declaration format of the blog controller class is as follows:
Class Blog extends Controller {
Function _ construct (){
Parent: Controller ();
}
}
4. Method declaration in control class: If the function name starts with an underscore (_), the function is a private method (private methods cannot be accessed through the URL ).
Set the private method name to _ remap:
This method will abolish the rule that the URI fragment determines which method is called and allow you to redefine the rule for calling the method (the routing rule of the method ).
Set the private method name to _ output:
This method will receive all the output data (Display Data) of its controller class, so that you can control the processing and output. This method is similar to the Destructor in OO. Whether you call any method, this method will always be executed.
5. System reserved class name: Controller CI_Base CI_Loader
6. System retention method name:
_ Ci_initialize
_ Ci_scaffolding
Index
Config
Database
Dbutil
Dbforge
File
Helper
Helpers
Language
Library
Model
Plugin
Plugins
Scaffolding
Script
View
Vars
_ Ci_assign_to_models
_ Ci_autoloader
_ Ci_init_class
_ Ci_init_scaffolding
_ Ci_is_instance
_ Ci_load
_ Ci_load_class
_ Ci_object_to_array
Is_really_writable ()
Load_class ()
Get_config ()
Config_item ()
Show_error ()
Show_404 ()
Log_message ()
_ Exception_handler ()
Get_instance ()
7. System reserved variables: $ config $ mimes $ lang
8. Reserved constants of the system:
EXT
FCPATH
SELF
BASEPATH
APPPATH
CI_VERSION
FILE_READ_MODE
FILE_WRITE_MODE
DIR_READ_MODE
DIR_WRITE_MODE
FOPEN_READ
FOPEN_READ_WRITE
FOPEN_WRITE_CREATE_DESTRUCTIVE
FOPEN_READ_WRITE_CREATE_DESTRUCTIVE
FOPEN_WRITE_CREATE
FOPEN_READ_WRITE_CREATE
FOPEN_WRITE_CREATE_STRICT
FOPEN_READ_WRITE_CREATE_STRICT

View)

A view is a webpage or part of a webpage, such as the header, bottom, and sidebar.
A view is never called directly and must be called by a controller.
1. View file storage location: application/views/
2. View File name: With. php extension
3. load the view in the controller class: $ this-> load-> view ('name '); // The name is the name of your view file (if the view file is saved in a subdirectory, it should also contain the subdirectory name, for example, subdirectory 1/try file name ). Note: There is no need to write the extension (suffix) of the. php file unless you use another extension.
4. Pass data to the View File: The data is transmitted to the view as an array or object through the controller. This array or object serves as the second parameter of the view loading function (for example: $ this-> load-> view ('name', array ('title' = & amp; gt; 'title ', 'content' => 'content '));). When loading multiple views at a time, you only need to input data in the first view.
5. Get the view content: set the third parameter of the $ this-> load-> view () function to "true", for example:
$ String = $ this-> load-> view ('myfile', ", true );
The third parameter in the view method indicates that the view is not output, but the result is returned to a variable.
Model)
The model class is declared in the same way as the controller class. The difference is that
1. Model file storage location: application/models/
2. Model class parent class name: Model
3. Different calling methods: (I) the controller class is called Through URL; (ii) the model class is used in the controller class:
$ This-> load-> model ('Model _ name ');
Reference: when referencing, the first parameter is the model class name (which may also contain subdirectory names); the second parameter is the new object name assigned after referencing; you can set the third parameter to TRUE (or an array containing the database connection configuration) to enable the model loading function to automatically connect to the database.
Helper functions (helpers)
1. Saving the auxiliary function File: system/helpers or system/application/helpers
2. Name of the auxiliary function File: yourname_helper.php (if it is your extension, add the prefix "MY _" or your custom prefix (application/config. php: $ config ['subclass _ prefix'] = 'My _';))
3. load a single helper function File: $ this-> load-> helper ('helper function filename '); // The helper function file name does not include the "_ helper. php" part.
4. load multiple auxiliary function files: $ this-> load-> helper (array ('auxiliary function file name 1 & prime;, 'auxiliary function file name 2 & prime ;, 'helper function file name 3 & prime ;));
5. Automatically load the auxiliary function File: you can open application/config/autoload. php and add the auxiliary function file name to the autoload array.
Plugins)
Plug-ins work almost exactly the same way as helper functions. The main difference between them is that the plug-in file generally has only one function, while the auxiliary function file usually contains a series of functions. Helper functions are seen as part of the core of the system, and plug-ins are usually made and shared by netizens.
1. Plug-in file storage location: system/plugins or system/application/plugins
2. File name of the plug-in File: yourname_pi.php
3. load a single plug-in: $ this-> load-> plugin ('Plug-In name'); // The plug-in name does not include the section "_ pi. php".
4. load multiple plug-ins: $ this-> load-> plugin (array ('plug-in name 1 & prime;, 'plug-in name 2 & prime ;, 'plug-in name 3 & prime ;));
5. Automatic loading plug-in: you can enable application/config/autoload. php and add plug-ins to the autoload array.
Library)
1. Storage location of the class library file: system/libraries (system class library) or system/application/libraries (custom class library)
2. Custom class library naming conventions: the class names and class file names must be consistent, and their first letters must be capitalized (if the class of the extended system class library needs to be prefixed with "MY _", you can set application/config. php: $ config ['subclass _ prefix']. Note: all original CodeIgniter class libraries use CI _ as the prefix. Therefore, do not use CI _ as your own prefix .)
3. File format
(I) fully custom class:
(Ii) class of extended system class library:
Class MY_Email extends CI_Email {
Function My_Email ()
{// If you need to use constructors in the class, you must explicitly inherit the parent class constructor in the constructor:
Parent: CI_Email ();
}
}
When you want to use the original CodeIgniter class in a custom class, you can do this:
First, define the CodeIgniter object to assign a variable:
$ CI = & get_instance (); // it must be referenced
Once an object is defined as a variable, you can replace $ this with the variable name:
$ CI = & get_instance ();
$ CI-> load-> helper ('URL ');
$ CI-> load-> library ('session ');
$ CI-> config-> item ('base _ url ');
Etc.

Homepage 1 2 3 4 Last page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.