TP5 THINKPHP5 Custom Exception handling classes

Source: Internet
Author: User

exception throwing in the development of a project is especially important not only to make friendly hints to help conceal the awkward moments of our great handlers, but also to prompt the developer code to write a white error, following the custom exception throw class, Purely personal understanding, I hope you correct me

First in the framework we can customize the directory structure to do the storage location of the exception class such as the file directory for the following red box

After defining the directory structure, I will explain the workflow of the exception Class I understand, since it is thrown when the code is out of the question or when the logic is abnormal, so I think there are two cases.

The first is the user transfer parameters or the user operation of the process has a problem in this case is not our development code or the business logic of the fatal error, it is straightforward that the user's request does not return the corresponding data

The second is the problem of our code, or the server above the problem, but this problem we do not need to know the user, in this case we need to give the user friendly hints, but also need to know where our code is the specific problem so in this case we need to log

After analyzing two cases, first create the class that should be the first case, named Baseexception.php in this class is very simple to define the first case of friendly information

The information includes the HTTP status code that throws the exception, the exception information, and the status code of the custom exception (optional)

But baseexception.php need to inherit exception although we are our own definition of the exception class but still need to follow the law, since to throw an exception, you need to have catch exception, baseexception.php code as follows

Let's take a look at the Official Handbook

Apparently, the authorities have pointed out what we're going to do next.

The definition z_exception.php is used to inherit the handle class and rewrite the render method so that we can throw our own custom information, and in this class, to be interspersed with the case of the second kind of exception

At this point, there is a noticeable problem in config.php

itself here is empty but we have defined the exception class ourselves, and it will lead to fatal error if it is not changed. Here we should pay attention.

Proceed to next add log after processing

Through the entry index.php can be traced to the core file of the framework start.php opened see load base.php then open base.php can see the file's configuration item find the path of the log view file

This allows you to navigate to the path of the log file

This allows us to change the path of log so that we manage it later and redefine the path of Log_path in index.php

After changing the path to the file, we should change the default log in the framework to refer to the official documentation

https://www.kancloud.cn/manual/thinkphp5/118126

Changing the logging in the frame is the way to find the log module in config.php, change the type of the journal from file to test

Since the log is designed to deal with problems in the production environment, I should write the code in the

The log is then logged, but we should use the log class in the framework if the logs are logged

Defining methods in z_exception.php to write to logs

and then call

This completes the custom class and the custom log file

The above is purely personal understanding, the shortcomings of the hope that everyone forgive!!!!

TP5 THINKPHP5 Custom Exception handling classes

Related Article

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.