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') );
?
?
??注:
(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理解为基于HTTP的REST软件架构,或者更进一步把REST和HTTP看作为等同的概念。

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools