PHPMVC framework error capture tutorial

Source: Internet
Author: User
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:

  1. Register_shutdown_function ('functionname ');
  2. 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:

  1. # Define the application path
  2. Define ('apppath', trim (_ DIR __,'/'));
  3. # Obtain the request address
  4. $ Root = $ _ SERVER ['script _ name'];
  5. $ Request = $ _ SERVER ['request _ URI '];
  6. $ URI = array ();
  7. # Obtain the address after index. php
  8. $ Url = trim (str_replace ($ root, '', $ request ),'/');
  9.  
  10. # If it is null, it is the access root address
  11. If (emptyempty ($ url ))
  12. {
  13. # Default controller and default method
  14. $ Class = 'index ';
  15. $ Func = 'Welcome ';
  16. }
  17. Else
  18. {
  19. $ URI = explode ('/', $ url );
  20.  
  21.  
  22. # If function is empty, index is accessed by default.
  23. If (count ($ URI) <2)
  24. {
  25. $ Class = ucfirst ($ URI [0]);
  26. $ Func = 'index ';
  27. }
  28. Else
  29. {
  30. $ Class = ucfirst ($ URI [0]);
  31. $ Func = $ URI [1];
  32. }
  33. }
  34.  
  35. # Load the class
  36. Include (APPPATH. '/'. 'application/controllers/'. $ class.'. php ');
  37.  
  38.  
  39. # Instantiation
  40. $ Obj = new $ class;
  41.  
  42.  
  43. Call_user_func_array (
  44. # Calling internal functions
  45. Array ($ obj, $ func ),
  46. # Passing parameters
  47. Array_slice ($ URI, 2)
  48. );

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.

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.