I have talked about various usage of phpmvc before. This article mainly introduces the usage of error capture in the PHPMVC framework. you can refer to the previous three articles that have made the structure and principles of MVC simple.
I have talked about various usage of php mvc before. This article mainly introduces the usage of error capture in PHP MVC framework. the first three articles have briefly explained and demonstrated the structure and principles of MVC... of course, it doesn't mean that a framework has only one thing. a good framework requires a wide range of functions, such as friendly error output, secure processing, and cross-origin attack prevention.
Okay, let's take a step. this section is about error capturing. In fact, it's not hard. you only need to understand two methods to do a good job of error capturing.
The instance code is as follows:
- Register_shutdown_function ('functionname ');
- Set_error_handler ('functionname ');
The first method is to register a shutdown callback function, which is called when the php script is closed. of course, this function can be executed normally, it can also be a script that throws a fatal error and closes the program. how can I differentiate it? Don't worry. there is another php method called error_get_last () to get the last php error message, as the function name says, in this way, you can determine whether the code is properly executed or fatal error...
The second method is to register an error callback function, which is called when an error occurs during script execution. the biggest difference between the two methods is that the first method is to close the call by the script, that is, one access, and only one call (because the script is aborted due to a fatal error ), second, when an error occurs during the trigger call of the second method, for example, accessing the index that does not exist in the array, using undefined variables is the capture of these warning and notice errors... if you want to know more about the differences between the two, visit the php official website to view the documentation... I will not post code here. if you are interested, you can go back to github and take a look...
The instance code is as follows:
- # Define the application path
- Define ('apppath', trim (_ DIR __,'/'));
- # Obtain the request address
- $ Root = $ _ SERVER ['script _ name'];
- $ Request = $ _ SERVER ['request _ URI '];
- $ URI = array ();
- # Obtain the address after index. php
- $ Url = trim (str_replace ($ root, '', $ request ),'/');
-
- # If it is null, it is the access root address
- If (emptyempty ($ url ))
- {
- # Default controller and default method
- $ Class = 'index ';
- $ Func = 'Welcome ';
- }
- Else
- {
- $ URI = explode ('/', $ url );
-
-
- # If function is empty, index is accessed by default.
- If (count ($ URI) <2)
- {
- $ Class = ucfirst ($ URI [0]);
- $ Func = 'index ';
- }
- Else
- {
- $ Class = ucfirst ($ URI [0]);
- $ Func = $ URI [1];
- }
- }
-
- # Load the class
- Include (APPPATH. '/'. 'application/controllers/'. $ class.'. php ');
-
-
- # Instantiation
- $ Obj = new $ class;
-
-
- Call_user_func_array (
- # Calling internal functions
- Array ($ obj, $ func ),
- # Passing parameters
- Array_slice ($ URI, 2)
- );
Let's take a look at the final effect... let's try to access methods in a non-existent controller. First, let's see if there is no error capture: then we can compare it with the error capture.