CodeIgniter配置之routes.php用法实例分析,codeigniterroutes
CodeIgniter配置之routes.php用法实例分析,codeigniterroutes
本文实例分析了CodeIgniter配置之routes.php用法。分享给大家供大家参考,具体如下:
application/config/routes.php中定义了一个名为$route的数组,用来设置默认路由和404页面以及可以设置一些匹配方式。
默认的配置如下:
$route['default_controller'] = "welcome"; $route['404_override'] = '';
default_controller指定默认的控制器名称,404_override指定当出现404时调用的控制器名称。有时候可能出现解析不成功,或者一直在默认页面,我们可以调用$this->router打印一下当前解析的控制器和Acion名称。比如可以在MY_Controller中如下打印:
var_dump($this->router->fetch_directory()); var_dump($this->router->fetch_class()); var_dump($this->router->fetch_method());
确定下解析到哪个控制器了, 然后在看看URL的配置、服务器配置,以及可以在Router.php 和URI.php中调试下。
$route数组也可以通过通配符(:num, :any)、正则来设置重写规则,下面是一些简单的例子:
1、将 http://pc.local/admin/detail_1.htm 请求解析到 http://pc.local/admin/detail.htm?user_id=1 处理。
Codeigniter并不支持包含查询字符串的重写规则,这个规则看起来应当这么写:
复制代码 代码如下:$route['admin/detail_(:num)'] = 'admin/detail?user_id=$1';
但实际上并未生效,程序匹配到admin/detail?user_id=1后用"/"分隔,索引为0的为控制器名,索引为1的为方法名,也就是会将上面的 detail?user_id=1赋值给方法名,结果可想而知就404了。搞清分隔原理后可以在detail后面增加一个斜杠,确保类名和方法名的正确,如:
复制代码 代码如下:$route['admin/detail_(:num)'] = 'admin/detail/?user_id=$1';
但此时又存在参数的获取问题了,会将第三个参数传递给方法,如果需要使用$_GET或者$this->input->get获取还需要对参数进行处理,如:
复制代码 代码如下:parse_str(ltrim($query_string, '?'), $_GET);
2、对PATH_INFO的URL形式重写规则还是比较支持的。如要实现http://pc.local/admin/1这种格式:
复制代码 代码如下:$route['admin/(:num)'] = 'admin/detail/$1';
参数的获取就只能通过段落的方式来获取了。
注意: 路由将会按照定义的顺序来运行.高层的路由总是优先于低层的路由.
最后,能使用CI来设置的路由还是建议使用CI来设置,不依赖服务器配置。
更多关于CodeIgniter框架相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
- MySQL的使用中实现读写分离的教程
- Yii实现多数据库主从读写分离的方法
- Thinkphp实现MySQL读写分离操作示例
- 使用PHP实现Mysql读写分离
- sql server2005实现数据库读写分离介绍
- MySQL主从同步、读写分离配置步骤
- mysql 读写分离(实战篇)
- mysql 读写分离(基础篇)
- CodeIgniter配置之SESSION用法实例分析
- Codeigniter控制器controller继承问题实例分析
- CodeIgniter读写分离实现方法详解

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: How to correctly configure Git in PyCharm In modern software development, the version control system is a very important tool, and Git, as one of the popular version control systems, provides developers with powerful functions and flexible operations. As a powerful Python integrated development environment, PyCharm comes with support for Git, allowing developers to manage code versions more conveniently. This article will introduce how to correctly configure Git in PyCharm to facilitate better development during the development process.

Title: The working principle and configuration method of GDM in Linux systems In Linux operating systems, GDM (GNOMEDisplayManager) is a common display manager used to control graphical user interface (GUI) login and user session management. This article will introduce the working principle and configuration method of GDM, as well as provide specific code examples. 1. Working principle of GDM GDM is the display manager in the GNOME desktop environment. It is responsible for starting the X server and providing the login interface. The user enters

PyCharm is a powerful integrated development environment (IDE), and PyTorch is a popular open source framework in the field of deep learning. In the field of machine learning and deep learning, using PyCharm and PyTorch for development can greatly improve development efficiency and code quality. This article will introduce in detail how to install and configure PyTorch in PyCharm, and attach specific code examples to help readers better utilize the powerful functions of these two. Step 1: Install PyCharm and Python

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

Maven is a Java project management and build tool that is widely used in the development of Java projects. In the process of using Maven to build projects, you often encounter some common environment configuration problems. This article will answer these common questions and provide specific code examples to help readers avoid common configuration errors. 1. Maven environment variables are incorrectly configured. Problem description: When using Maven, if the environment variables are incorrectly configured, Maven may not work properly. Solution: Make sure

PyCharm is a commonly used integrated development environment (IDE). In daily development, using Git to manage code is essential. This article will introduce how to configure Git in PyCharm and use Git for code management, with specific code examples. Step 1: Install Git First, make sure Git is installed on your computer. If it is not installed, you can go to [Git official website](https://git-scm.com/) to download and install the latest version of Git

MyBatisGenerator is a code generation tool officially provided by MyBatis, which can help developers quickly generate JavaBeans, Mapper interfaces and XML mapping files that conform to the database table structure. In the process of using MyBatisGenerator for code generation, the setting of configuration parameters is crucial. This article will start from the perspective of configuration parameters and deeply explore the functions of MyBatisGenerator.

How to configure a workgroup in Win11 A workgroup is a way to connect multiple computers in a local area network, which allows files, printers, and other resources to be shared between computers. In Win11 system, configuring a workgroup is very simple, just follow the steps below. Step 1: Open the "Settings" application. First, click the "Start" button of the Win11 system, and then select the "Settings" application in the pop-up menu. You can also use the shortcut "Win+I" to open "Settings". Step 2: Select "System" In the Settings app, you will see multiple options. Please click the "System" option to enter the system settings page. Step 3: Select "About" In the "System" settings page, you will see multiple sub-options. Please click
