Home Backend Development PHP Tutorial CakePHP中Router的机制跟使用方法

CakePHP中Router的机制跟使用方法

Jun 13, 2016 pm 01:04 PM
cakephp controller rest router url

CakePHP中Router的机制和使用方法

一、典型CakePHP处理 Request 的流程


?? ? ?当人浏览网站,发出请求,.htaccess文件将request映射到webroot/index.php文件,此文件中主要处理:1.常量的定义;2.包含bootstrap.php文件;3.调用 dispather() 函数。

?

二、认识 Router 与 Dispatcher、Controller 之间的关系

?? ? ?(1). 路由(Router)从URL中解释并提取出这次请求的参数:控制器(controller)、动作(action)以及其他在这次请求中关联影响到得业务逻辑的所有其他参数信息;

?? ? ?(2).使用路由后,请求的URL被映射到一个控制器的动作中(在特定的一个控制器类得一个方法)


?? **** 部分函数解析 ****

?? 1. Router::parse($url)

?? ? ? 根据提供的url,解析出对应的controller、action、params 的数组。所依据的准则:默认路由、新定义创建的路由规则。

?

?

?? 2. Router::getArgs($args, $options = array()) 将passed params 转换为参数

?

三、CakePHP 中Router 配置

?? ? CakePHP 源码包中 router.php 文件:



??自定义路由:

connect() 函数的三个参数:1. 你希望匹配的URL值;2. 自定义的路由元素的默认值;3. 帮助该路由器匹配的URL元素的正则规则。

基本格式用于Router定义:

?

Router::connect(
    'URL',
    array('paramName' => 'defaultValue'),
    array('paramName' => 'matchingRegex')
);
Copy after login

?

?

??注:

(1) 定义URL类型。URL通常由斜杠分割的,但是也会包含一些通配符(*)或者自定义路由元素(URL元素以冒号为前缀) 用通配符来匹配URL的类型。特殊化的路由元素允许你为你的控制器actions收集参数。

(2) 当请求出现时,这些路由元素的值会在控制器的 <span style="font-size: 12.0pt;">$this->params</span> 存在。这不同于命名参数 (named parameter),所以注意区别:命名参数 (/controller/action/name:value) 存在于 <span style="font-size: 12.0pt;">$this->passedArgs</span> 中,而自定义路由元素存在于 <span style="font-size: 12.0pt;">$this->params</span>

?(3) 默认路由:/:controller、 /:controller/:action/*

?

?? ?这样的配置会返回什么呢???

?? ?写两个例子看一下:


?

四、 CakePHP 中Router 的应用

??(1). 简单应用:


??(2). 高级应用:




?? ? ?SlugRoute的解析方法很简单。首先我们调用父类parse()函数,可得到解析正则表达式的路径。任何空的或false的返回都会导致我们解析路径失败(路径不匹配),只有当我们得到有效的路径(对于post带有匹配的参数).自从CakeRoute:parse()将已经创造了参数,所以我们不需要修改参数,返回他们如果没有目标存在或返回错误。

?? ? ?<span style="font-size: 12.0pt;">app/config/routes.php</span><span style="font-size: 12.0pt;">中</span>引用此router类:

?

五、 CakePHP 中Router 配置文件的弊端

?? ? ? CakePHP的缺省实现中,我们并不用去设置Route,系统会遵循/controller/action/param的形式去解析URL。但是很多时候这样的URL并不友好,于是我们就要设置自定义的Route,其结果就是Route配置文件不断膨胀。值得注意的是PHP对于每个请求都要重建运行环境,而对于一次请求而言,Route配置文件里最后使用的只是一条配置而已,为了这一条配置,却不得不重复解析整个庞大的Route配置文件,无疑是个巨大的负担。

?? ? ? REST风格是解决这个问题的好办法,采用REST风格的架构方式,资源的URL形式相对确定,操作方式由HTTP方法而定,因此Route配置文件会迅速瘦身。



?? ? ??REST软件架构是一个抽象的概念,是一种为了实现这一互联网的超媒体分布式系统的行动指南。利用任何的技术都可以实现这种理念。而实现这一软件架构最著名的就是HTTP协议。通常我们把REST也写作为REST/HTTP,在实际中往往把REST理解为基于HTTPREST软件架构,或者更进一步把RESTHTTP看作为等同的概念。

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

PHP function introduction—get_headers(): Get the response header information of the URL PHP function introduction—get_headers(): Get the response header information of the URL Jul 25, 2023 am 09:05 AM

PHP function introduction—get_headers(): Overview of obtaining the response header information of the URL: In PHP development, we often need to obtain the response header information of the web page or remote resource. The PHP function get_headers() can easily obtain the response header information of the target URL and return it in the form of an array. This article will introduce the usage of get_headers() function and provide some related code examples. Usage of get_headers() function: get_header

Why NameResolutionError(self.host, self, e) from e and how to solve it Why NameResolutionError(self.host, self, e) from e and how to solve it Mar 01, 2024 pm 01:20 PM

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

See all articles