Php mvc framework error capture tutorial

Source: Internet
Author: User

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 capture. In fact, it is not difficult. You only need to understand two methods to do a good job of error capture.

The code is as follows: Copy code


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 normally executed or fatal error occurs...

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 indexes that do 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 code is as follows: Copy code

<? Php

# 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 (empty ($ 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 controller that does not exist.

First, let's see if there is no error capture:

 

Next, we will compare the error capture.

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.