First of all, this problem was accidentally seen in the Yaf group. These friends are not so comfortable. Today, they are idle and scan the code to come to a conclusion.
Second, the other point of view is that tools must be clear about their principles when used, or they will not be saved.
First, let's take a look at the Yaf-based PHP code example:
| The code is as follows: |
Copy code |
|
<? Php
Use Yaf \ Dispatcher;
Use Yaf \ Controller_Abstract;
Final class IndexController extends Controller_Abstract
{
Public function init ()
{
// None
}
Public function indexAction ()
{
$ Request = new Yaf \ Request \ Simple ('api ', 'index', 'index', 'simple ');
Dispatcher: getInstance ()-> dispatch ($ request );
$ This-> _ view-> assign ('displaywelcome ', 1 );
$ This-> _ view-> assign ('hello', 'Hello world! ');
}
Public function simpleAction ()
{
Echo 'xudianyang ';
}
} |
In the default indexAction, I created a Yaf \ Request \ Simple request object $ Request. The parameter indicates the Simple action of the request Index module and the Index controller, it should be noted that if you set the request to be the same as the routing parameter of the current action, you will enter an endless loop and you will not need to give it a thumbs up. Then, call the dispatch method of the Dispatcher for route distribution.
From the figure, we can see that in the process of "a new round of distribution", any link has problems, or exit, return will not cause program crash. For example, the simpleAction method is written as follows:
| The code is as follows: |
Copy code |
|
Public function simpleAction ()
{
Echo 'xudianyang ';
Exit;
} |
For another example, the template file views/index/simple. phtml corresponding to the simple method does not exist and does not produce Segmentation fault.
The cause of the segment error is also obvious in the figure. Pointer reference is no longer available, because the Dispatcher: getInstance ()-> dispatch ($ request) is called in the next distribution ); this method resets the $ _ request attribute of the Yaf \ Dispatcher single instance.
You can also use gdb to debug the core file:
| The code is as follows: |
Copy code |
(Gdb) bt
#0 0x083d58d4 in zend_std_read_property (object = 0xb777c020, member = 0xb73f8938, type = 3, key = 0x0)
At/home/vagrant/php-5.4.31/Zend/zend_object_handlers.c: 431
#1 0x083b00008 in zend_read_property (scope = 0xa392cb8, object = 0xb777c020, name = 0xcb64c6 "module", name_length = 6, silent = 1' \ 001 ')
At/home/vagrant/php-5.4.31/Zend/zend_API.c: 3859
#2 0x00c96c85 in yaf_dispatcher_fix_default (dispatcher = 0xb778143c) at/home/vagrant/yaf-2.3.2/yaf_dispatcher.c: 236
#3 yaf_dispatcher_dispatch (dispatcher = 0xb778143c) at/home/vagrant/yaf-2.3.2/yaf_dispatcher.c: 927
#4 0x00c91bd6 in zim_yaf_application_run (ht = 0, return_value = 0xb7799fcc, return_value_ptr = 0x0, this_ptr = 0xb777a140,
Return_value_used = 0) at/home/vagrant/yaf-2.3.2/yaf_application.c: 468
#5 0x0841f5d4 in zend_do_fcall_common_helper_SPEC (execute_data =)
At/home/vagrant/php-5.4.31/Zend/zend_vm_execute.h: 643
#6 0x084137a6 in execute (op_array = 0xb777a408) at/home/vagrant/php-5.4.31/Zend/zend_vm_execute.h: 410
#7 0x083b1c6e in zend_execute_scripts (type = 8, retval = 0x0, file_count = 3) at/home/vagrant/php-5.4.31/Zend/zend. c: 1315
#8 0x08357706 in php_execute_script (primary_file = 0xbfb72338) at/home/vagrant/php-5.4.31/main. c: 2502
#9 0x0845c1cb in main (argc = 6, argv = 0xbfb72494) at/home/vagrant/php-5.4.31/sapi/fpm/fpm_main.c: 1933
|
Finally, the analysis is just to familiarize yourself with the Yaf distribution policy and its source code. In addition, if you want to execute another action in an action, you can use Yaf_Controller_Abstract: forward. In practical applications, we should not be confused!