Source code notes of Yaf framework learning Notes

Source: Internet
Author: User
Tags php error

 

Soon after learning about PHP, I thought about using extensions to implement the PHP program framework. Later I learned that laruence has implemented the yaf simple C to implement the PHP MVC framework, the source code has been studied several times. Yaf has the advantages of being simple, efficient, high-performance, long-standing framework memory, and memory-based program configuration. In fact, it runs faster than most PHP frameworks.

1. Load PHP user space files in the kernel extension

File: yaf_loader.c
Function: int yaf_loader_import (char path, int len int use_path TSRMLS_DC)
Row: 333

2. Automatic loader
Principle: use spl_autoload_register to automatically load classes
File: yaf_application.c
Function: PHP_METHOD yaf_application _ construct
Row: 390

File: yaf_loader.c
Function: yaf_loader_t * yaf_loader_instance (yaf_loader_t * this_ptr, char * library_path, char * global_path TSRMLS_DC)
Row: 321

Function: int yaf_loader_register (yaf_loader_t * loader TSRMLS_DC)
Row number: 73

Function: PHP_METHOD (yaf_loader, autoload)
Row: 695

2. Format the string and output it to the string pointer.
Char * directory = NULL;
Spprintf (& directory, 0, "% d/% s", 45, "xy", "AB ");

Then: directory is 45/xy/AB

3. smart_str struct
File: yaf_loader.c
Function: int yaf_internal_autoload (char file_name uint name_len char directory TSRMLS_DC)
Row: 398

4. zend_file_handle struct
File: yaf_loader.c
Function: int yaf_loader_import (char path int len int use_path TSRMLS_DC)
Row: 334

5. zend_compile_file
File: yaf_loader.c
Function: int yaf_loader_import (char path int len int use_path TSRMLS_DC)
Row: 348

6. Both plugin and route need to be manually added in the PHP user space, for example:
Add plugin to the bootstrap file
$ User_plugin = new UserPlugin ();
$ Dispatcher-> registerPlugin ($ user_plugin );

Add route to the bootstrap file
// Obtain application configuration information
$ Config = YafApplication: app ()-> getConfig ();
// Register the configuration
YafRegistry: set ("config", $ config );
// Obtain the vro instance through the dispatcher
$ Router = $ dispatcher-> getRouter ();
// Add the router protocol through the configuration file
$ Router-> addConfig (YafRegistry: get ('config')-> routes );

7. init method of Yaf_Controller_Abstract
If the controller that inherits Yaf_Controller_Abstract defines the init method, the init method will be called during controller initialization.

File: yaf_controller.c
Function: int evaluate (zend_class_entry * ce, yaf_controller_t * self, yaf_request_t * request, yaf_response_t * response, yaf_view_t * view, zval * args TSRMLS_DC)
Row: 230

8. different from the general framework, you can define action parameters in Yaf. The values of these parameters come from the same name parameter values in the Request route results, however, you can check the Yaf source code and find that only Yaf_route_static has implemented the above action parameters.

9. Yaf_Controller_Abstract: forward only registers the destination to be forward and does not jump to immediately. Instead, it will wait until the execution of the current Action is complete before launching a new dispatch.

10. Dispatcher modifies the module, controller, and action of the route result. If no module is specified in the request url, the module is changed to the default module. For example, the initial letter size of the module and controller, and action is converted to lowercase letters.
File: yaf_dispatcher.c
Function: static inline void yaf_dispatcher_fix_default (yaf_dispatcher_t * dispatcher, yaf_request_t * request TSRMLS_DC)
Row: 233

11. Sometimes, to split a large Controller, the code is clearer and easier to manage. Yaf supports defining specific actions separately.

12. yaf exception mode
When Application. dispatcher. throwException = 1, yaf will change the PHP error trigger to an exception and throw it when Application Configuration sets application. dispatcher. catchException = 1. When yaf detects an uncaptured exception, yaf grants the running permission to the errorAction in the ErrorController of the current module.

In the source code, check the exceptions member status of the EG (exception) PHP global struct _ zend_executor_globals struct to determine whether the business code has encountered an exception. Note that it is the business code, that is, distribution starts from the distributor Dispatcher, that is, when YafApplication-> run () calls yaf_dispatcher_dispatch (), the Action detection code is executed at each distribution link to check whether an exception is found, this is because macro YAF_EXCEPTION_HANDLE is called after each distribution stage is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.