Table of Contents
CodeIgniter框架URL路由总结,codeigniter框架url
PHP CodeIgniter框架中怎获取当前页面的URL
怎删除CodeIgniter框架中URL段里面的indexphp,实例,手册上的木看明白,自己写了个dome测试也不对
Home php教程 php手册 CodeIgniter框架URL路由总结,codeigniter框架url

CodeIgniter框架URL路由总结,codeigniter框架url

Jun 13, 2016 am 09:25 AM
codeigniter url routing

CodeIgniter框架URL路由总结,codeigniter框架url

URI 路由

一般来说,URI字符串有着和它唯一对应的控制器(controller)类/方法。URI的各个部分是如下模式(pattern):

复制代码 代码如下:


example.com/class/function/id/


然而在一些例子中,你也许想重定向这种关系来调用一个不同的类/方法(class/function),而不是与URL一一对应(的调用).

例如,你可能想使你的URL采用这种原型(prototype):

复制代码 代码如下:


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

一般情况下,URL的第二个部分表示方法名,不过在上面的例子中,它表示一个产品的ID。CodeIgniter可以实现这个功能,让用户可以重新定向(remap)URI处理程序.

设定你自己的路由规则

  路由规则定义在application/config/routes.php 文件中. 在此文件中,你可以看到一个名为 $route的数组,它可以让你定义你自己的路由规则。 定义可以用两种方式: 通配符(wildcards) 或者 正则表达式(Regular Expressions)

通配符

  一个典型的通配符路由看起来是这样的:

复制代码 代码如下:


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

  在一个路由中,数组的键包含着被匹配的URI,而数组的值包含着路由将被重定向的目的地.在上面的例子中,如果单词“product”出现在URL的第一个部分中,而且数字(:num)出现在URI的第二个部分中,"catalog"类和"product_lookup"方法将被替代使用(即将被重定向).

  你可以匹配文字的值或者使用以下两种通配符类型:

  :num 将匹配一个只包含有数字的segment(段).

  :any 将匹配任何字符(可以是多个segment段).可以匹配多个值,如:

  $route['product/(:any)'] = "catalog/product_lookup/$1/$2/$3/$4/$5"; //将整条url上的每一个参数全部传递给catalog控制器下的 product_lookup方法。

  注意: 路由将会按照定义的顺序来运行.高层的路由总是优先于低层的路由.

例子

下面是一些简单的例子:

复制代码 代码如下:


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


  如果URL的第一个分段(类名)是关键字"journals",那么将会重定向到"blogs"类中处理.

复制代码 代码如下:


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


  如果URL的前两个分段是"blog"和"joe",那么将会重定向到"blogs"类的"users"方法中处理.并且将ID"34"设为参数.

复制代码 代码如下:


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


  当"product"作为URL中第一个分段时, 无论第二分段是什么都将被重定向到"catalog"类的"product_lookup"方法.

复制代码 代码如下:


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

  当“product”作为 URL 中第一个分段时,如果第二分段是数字,则将被重定向到“catalog”类,并传递所匹配的内容到“product_lookup_by_id”方法中。

  重要提示: 不要在前面或后面加"/".

  正则表达式

  如果你喜欢可以使用正则表达式来自定义你的路由规则. 任何有效的正则表达式都是被允许的, 甚至逆向引用.

  注意: 如果你使用逆向引用请将双反斜线语法替换为美元符语法(\\1 替换为 $1).

  一个典型的正则表达式看起来像下面的样子:

复制代码 代码如下:


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

上例中, 类似于 products/shirts/123 的URI 将换成调用 shirts 控制器类的 id_123 方法.

你也可以混合使用通配符与正则表达式.

系统保留的路由

系统会保留两个路由:

第一个为系统默认的路由:

复制代码 代码如下:


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

这个路由表明了当URI中不包含要访问的类和控制器信息的(即只访问根目录的情况,如http://localhost/ci)将要加载哪个控制器。上例中,系统将加载 "welcome" 这个类(控制器)。你应该保证设置一个默认路由,不然你的首页将会显示 404 错误。

第二个为404页面的路由:

复制代码 代码如下:


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

这个路由标识了如果请求的控制器无法访问的时候将加载哪个控制器。它相当于覆盖了默认的404错误页面(即提供了自己定义404页面的功能)。但它不会影响show_404()这个方法,这个方法依然会加载默认的位于application/errors/error_404.php的error_404.php页面。

重要: 保留的路由应该在所有通配符或正则表达式路由之前定义。

PHP CodeIgniter框架中怎获取当前页面的URL

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

怎删除CodeIgniter框架中URL段里面的indexphp,实例,手册上的木看明白,自己写了个dome测试也不对

要想实现这个只能用伪静态来实现。.htaccess这个配置规则实现
 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement custom middleware in CodeIgniter How to implement custom middleware in CodeIgniter Jul 29, 2023 am 10:53 AM

How to implement custom middleware in CodeIgniter Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request

CodeIgniter middleware: Accelerate application responsiveness and page rendering CodeIgniter middleware: Accelerate application responsiveness and page rendering Jul 28, 2023 pm 06:51 PM

CodeIgniter Middleware: Accelerating Application Responsiveness and Page Rendering Overview: As web applications continue to grow in complexity and interactivity, developers need to use more efficient and scalable solutions to improve application performance and responsiveness. . CodeIgniter (CI) is a lightweight PHP-based framework that provides many useful features, one of which is middleware. Middleware is a series of tasks that are performed before or after the request reaches the controller. This article will introduce how to use

How to use FastRoute for URL routing in php? How to use FastRoute for URL routing in php? Jun 02, 2023 am 11:40 AM

Routing is an inevitable part of modern web development. Routing serves as a bridge between web pages and applications, connecting HTTP requests and specific operations in the application. The purpose of routing is to map incoming URL requests to appropriate handlers so that we can initialize a page or perform other operations based on the URL request. In PHP, the routing system is usually implemented by using a third-party library called "FastRoute". FastRoute is a high-performance PHP routing library. it can

Implementing an efficient URL routing resolution solution in PHP Implementing an efficient URL routing resolution solution in PHP Oct 15, 2023 pm 04:20 PM

Implementing an efficient URL route resolution solution in PHP When developing web applications, URL route resolution is a very important link. It can help us implement a friendly URL structure and map requests to the corresponding handlers or controllers. This article will introduce an efficient URL routing resolution solution and provide specific code examples. 1. The basic principle of URL route parsing The basic principle of URL route parsing is to split the URL into different parts and match and map them based on the contents of these parts. Common UR

How to use the database query builder (Query Builder) in the CodeIgniter framework How to use the database query builder (Query Builder) in the CodeIgniter framework Jul 28, 2023 pm 11:13 PM

Introduction to the method of using the database query builder (QueryBuilder) in the CodeIgniter framework: CodeIgniter is a lightweight PHP framework that provides many powerful tools and libraries to facilitate developers in web application development. One of the most impressive features is the database query builder (QueryBuilder), which provides a concise and powerful way to build and execute database query statements. This article will introduce how to use Co

PHP development: Using CodeIgniter to implement MVC pattern and RESTful API PHP development: Using CodeIgniter to implement MVC pattern and RESTful API Jun 16, 2023 am 08:09 AM

As web applications continue to evolve, it is important to develop applications more quickly and efficiently. And, as RESTful API is widely used in web applications, it is necessary for developers to understand how to create and implement RESTful API. In this article, we will discuss how to implement MVC pattern and RESTful API using CodeIgniter framework. Introduction to MVC pattern MVC (Model-Vie

How to use CodeIgniter5 framework in php? How to use CodeIgniter5 framework in php? Jun 01, 2023 am 11:21 AM

CodeIgniter is a lightweight PHP framework that uses MVC architecture to support rapid development and simplify common tasks. CodeIgniter5 is the latest version of the framework and offers many new features and improvements. This article will introduce how to use the CodeIgniter5 framework to build a simple web application. Step 1: Install CodeIgniter5 Downloading and installing CodeIgniter5 is very simple, just follow these steps: Download the latest version

CodeIgniter middleware: Provides secure file upload and download functions CodeIgniter middleware: Provides secure file upload and download functions Aug 01, 2023 pm 03:01 PM

CodeIgniter middleware: Provides secure file upload and download functions Introduction: In the process of web application development, file upload and download are very common functions. However, for security reasons, handling file uploads and downloads often requires additional security measures. CodeIgniter is a popular PHP framework that provides a wealth of tools and libraries to support developers in building secure and reliable web applications. This article will introduce how to use CodeIgniter middleware to implement secure files

See all articles