CodeIgniter framework URL routing summary, codeigniter framework url_PHP tutorial

WBOY
Release: 2016-07-13 10:19:31
Original
735 people have browsed it

CodeIgniter framework URL routing summary, codeigniter framework url

URI routing

Generally speaking, a URI string has a unique corresponding controller class/method. The various parts of the URI are the following patterns:

Copy code The code is as follows:

example.com/class/function/id/

However, in some cases, you may want to redirect this relationship to call a different class/function instead of corresponding to the URL.

For example, you might want to use this prototype for your URL:

Copy code The code is as follows:

example.com/product/1/
example.com/product/2/
example.com/product/3/
example.com/product/4/

Normally, the second part of the URL represents the method name, but in the above example, it represents a product ID. CodeIgniter can implement this function, allowing users to redirect (remap) URI handlers.

Set your own routing rules

Routing rules are defined in the application/config/routes.php file. In this file, you can see an array named $route, which allows you to define your own routing rules. Definitions can be used in two ways: wildcards or regular expressions

Wildcard

A typical wildcard route looks like this:

Copy code The code is as follows:

$route['product/(:num)'] = "catalog/product_lookup";

In a route, the array key contains the URI to be matched, and the array value contains the destination to which the route will be redirected. In the above example, if the word "product" appears first in the URL parts, and the number (:num) appears in the second part of the URI, the "catalog" class and "product_lookup" method will be used instead (will be redirected).

You can match literal values ​​or use the following two wildcard types:

 :num will match a segment containing only numbers.

 :any will match any character (can be multiple segments). Can match multiple values, such as:

 $route['product/(:any)'] = "catalog/product_lookup/$1/$2/$3/$4/$5"; // Pass every parameter on the entire url to the catalog controller product_lookup method.

Note: Routes will be run in the order defined. Higher-level routes always take precedence over lower-level routes.

Example

Here are some simple examples:

Copy code The code is as follows:

$route['journals'] = "blogs";

If the first segment (class name) of the URL is the keyword "journals", it will be redirected to the "blogs" class for processing.

Copy code The code is as follows:

$route['blog/joe'] = "blogs/users/34";

If the first two segments of the URL are "blog" and "joe", then it will be redirected to the "users" method of the "blogs" class for processing. And the ID "34" will be set as the parameter.

Copy code The code is as follows:

$route['product/(:any)'] = "catalog/product_lookup";

When "product" is used as the first segment in the URL, no matter what the second segment is, it will be redirected to the "product_lookup" method of the "catalog" class.

Copy code The code is as follows:

$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";

When "product" is the first segment in the URL, if the second segment is a number, it will be redirected to the "catalog" class and the matched content will be passed to the "product_lookup_by_id" method.

Important note: Do not add "/" before or after.

Regular expression

If you like, you can use regular expressions to customize your routing rules. Any valid regular expression is allowed, even reverse references.

Note: If you use backreferences please replace the double backslash syntax with dollar sign syntax (\1 is replaced by $1).

A typical regular expression looks like the following:

Copy code The code is as follows:

​$route['products/([a-z]+)/(d+)'] = "$1/id_$2";

In the above example, a URI similar to products/shirts/123 will be replaced by calling the id_123 method of the shirts controller class.

You can also mix wildcards with regular expressions.

Routes reserved by the system

The system will retain two routes:

The first one is the system default route:

Copy code The code is as follows:

$route['default_controller'] = 'welcome';

This route indicates which controller will be loaded when the URI does not contain the class and controller information to be accessed (that is, when only the root directory is accessed, such as http://localhost/ci). In the above example, the system will load the "welcome" class (controller). You should make sure to set a default route, otherwise your homepage will show a 404 error.

The second one is the route for the 404 page:

Copy code The code is as follows:

$route['404_override'] = '';

This route identifies which controller will be loaded if the requested controller is inaccessible. It is equivalent to overwriting the default 404 error page (that is, providing the function of defining your own 404 page). But it will not affect the show_404() method, which will still load the default error_404.php page located at application/errors/error_404.php.

Important: Reserved routes should be defined before any wildcard or regular expression routes.

How to get the URL of the current page in the PHP CodeIgniter framework

//use url helper
$this->load->helper('url');
current_url = current_url();

How to delete the indexphp in the URL segment in the CodeIgniter framework, examples, the manual is not clear, even if I write a dome test myself, it is wrong

To achieve this, we can only use pseudo-static. .htaccess configuration rule implementation

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/874110.htmlTechArticleCodeIgniter framework URL routing summary, codeigniter framework url URI routing Generally speaking, the URI string has its unique corresponding Controller class/method. The various parts of the URI are as follows...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template