PHP 框架 Yaf 学习笔记----默认行为
Yaf是一个C语言编写的PHP框架,以php扩展的形式. 是 laruence(鸟哥)? 的作品
laruence 是PHP 开发组成员, PECL 开发者. Yaf, Taint等Pecl扩展作者.
Yaf 相关文章 http://www.laruence.com/tag/yaf? ? 在线手册
在线手册系统完整的阐述了Yaf的一切,这里作为学习导读,列出Yaf的默认行为(契约)
0.Yaf 配置文件(php.ini风格)
其实分两部分,一个是php.ini中的配置 "yaf." 开头,这部分通常是不用调整的。
另一部分是Yaf_Application初始化时刻的配置 "application." 开头
?
配置可以引入php常量定义
1.Yaf 的必须配置,其实就是个目录设定
yaf.directory=APP_PATH application.directory=APP_PATH
也就是 appliction 脚本目录,至少要定义一个,application.directory优先
2.Yaf 有一套完整的目录组织,文件命名规则
所有Yaf应用都应该遵循下面的目录组织结构
- index.php //入口文件 - .htaccess //重写规则 + conf |- application.ini //配置文件 application/ + controllers - Index.php //默认控制器 + views |+ index //控制器 - index.phtml //默认视图 + modules //其他模块 - library - models //model目录 - plugins //插件目录?
Yaf支持大小写敏感和不敏感两种方式,但是我没有找到大小写敏感的用法(linux环境)
类命名和搜索规则
类名称???????????? 对应目录
ClassModel????? models
ClassPlugin????? plugins
ClassController controllers
其他??????????????? library
3.Yaf 是通过以包含分隔符"_"的类名称配合目录组织,和完整的类命名规则完成类加载管理
比如以前你有一个Bar类文件Bar.php,如果用到Yaf里,并自动加载,那么就要这样组织
把Bar.php放到library目录下的一个子目录里面,比如 myclass
并且要更改Bar.php里面的类名(一般都是Bar的)为MyClass_Bar
也就是说类名必须包含目录名 具体解释
4.错误处理
Yaf里面错误处理可以通过一个控制器完成,声明在 APP_PATH/controllers/Error.php 里
class ErrorController extends Yaf_Controller_Abstract {...}
当然错误控制器是否生效受配置影响
application.dispatcher.throwException=1 application.dispatcher.catchException=1
具体看 官方提供的例子
5.视图view
render视图需要指定视图模板文件,可以用相对(application/views)路径或者绝对路径
6.路由
Yaf提供了多种级别的路径支持,正则路由也有支持,你可以用配置文件写路由,或者在Bootstrap.php里面用php脚本写路由
个人认为Yaf最吸引人的地方是:
PHP Framework in PHP extension,如果没有数据库访问的瓶颈造成的木桶效应,Yaf这种形式是最优的
使用Yaf后在某种级别的应用中内存占用应该降低很多
?
7.BUG?发现如下问题,不知道是否是bug
Yaf在匹配配置文件的节(section)的时候,不是对名称完全匹配,比如配置里面有
[product] ...
new Yaf_Application("conf/app.ini",'p')//照样可以被匹配到
?
8.MVC
Yaf和大多数框架一样提供了一个默认的MVC行为,对于M(setDefaultModule)我没有找到更改生效的方法。
C和V是一一对应的,当然可以名称不同,可以通过Yaf_Dispatcher的setView方法进行设置。
?
9.单例模式类,Yaf中
Yaf_Application
Yaf_Loader
Yaf_Dispatcher
Yaf_Registry
Yaf_Session
类实例
是单例,其他不是
需要注意如果在Yaf_Controller_Abstract继承类action中使用了forward,那将会产生新的实例对象。
?
10.非嵌套Dispatcher(2.x版本以下)
Yaf对请求的处理是非嵌套的,这意味着所有对
Controller转移都是非嵌套的,如果一个Controller没有执行完,发生了转移,那有可能发生不可知的结果甚至错误,Yaf提供的
forward是等待当前Controller执行完毕才处理的,因此如果完全按照MVC的方式对请求进行处理,要达到嵌套处理的效果,需要自己实现代码.流程对应说明
mainloopstart dispatchLoopStartup preDispatch//其实是个循环受forward控制 这之间request不能发送改变,可以forward但是会被延期执行 postDispatch forward的request延期执行,循环到preDispatch dispatchLoopShutdown 可以dispatch一个新的request,会开始新的dispatchLoopStartup mainloopend?
?

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.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


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

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment