The project team recently introduced the laravel framework. I participated in laravel's research and project architecture design. I personally think that some of the Design Based on laravel in the project architecture is more practical and can be used for reference. I will share some design with you and hope to learn and discuss it with you. In particular, this article does not extract or summarize lavarel official documents.
1. Exception Handling
1.1 exception
Exception classes are stored in APP/lib/exception, which can be subdivided according to the business module. The abbreviated exception classes can be in the form of multiple exception classes in one file, for example:
Class httprequestexception extends exception
{
}
Class httpresponseexception extends exception
{
}
1.2 capture mechanism
Exception capture can be performed wherever needed. If not captured, the exception will be thrown to the outermost layer.
The handler is defined in the app/start/global. php file.
Function handleexception ($ code, $ exception)
{
Return decorate: Failed ($ code, null, $ exception-> GetFile (). ':'. $ exception-> Getline (). ','. $ exception-> getmessage ());
}
APP: Error (function (httprequestexception $ exception, $ code)
{
Return handleexception (-1007, $ exception );
});
APP: Error (function (httpresponseexception $ exception, $ code)
{
Return handleexception (-1008, $ exception );
});
1.3 throw Mechanism
An exception can be thrown wherever an exception can be triggered.
Requestlog: Request ($ URL, $ data, $ start, $ content );
If (! $ Content ){
Throw new httprequestexception ($ URL. ':'. $ data );
}
2 log records
There are three types of logs: API call log (requestlog), Service Log (logicaltlog), and debug log (debuglog ). Logs are stored in the app/lib/log directory. Here, requestlog can be used for statistical analysis of interface calls, logicaltlog can be used to record logical data, and debuglog is used to output debugging information (you can also directly use the \ log class that comes with laravel ).
3. task queue
Laravle encapsulates the queue for task queue, which is very convenient for asynchronous processing. Supported: "sync", "beanstalkd", "SQS", "iron", and "redis. We recommend using redis, which is super easy to use.
The queue usage method can be used as long as the class name of the task class is pushed to the queue and the task class implements the fire method.
In fire ($ job, $ data), we can also get the number of task attempts $ job-> attempts (), you can delay the task response time $ job-> release (30); you can also delete the task $ job-> Delete ();.
Note: You can use the artisan tool of the laravel framework to start the queue listener:
PHP.../artisan -- Env = devqueue: Listen &
4 Filter
Filter can be used for parameter verification, login status check, and interface call logs.
4.1 parameter check
Define the parameter verification conditions for each interface in APP/config/Param. php. For the verification conditions, see laravel documentation.
Then, in the before of APP/filter. php, verify the parameters of each call, for example:
APP: Before (function ($ request ))
{
...
$ Res = Param: verification (input: All (), $ standard );
...
}
4.2 API call logs
APP: After (function ($ request, $ response)
{
...
Requestlog: log ($ request, $ response );
...
});
5. Environment Switching
Generally, our framework has several environments: formal, testing, local, sandbox, etc. The configuration of different environments must be different. Laravel allows you to specify the current configuration environment when the process starts to automatically switch between different environments.
1) Different Environment configuration directories:
APP \ config \ Dev
APP \ config \ formal
APP \ config \ Local
2) bootstrap/start. php specifies the required environment, such as the test environment Dev.
$ ENV = $ app-> detectenvironment ('dev ')
3) How to automatically switch?
We can specify the corresponding environment based on the domain name requested by the interface. For example, dev.domain.com is the test environment and domain.com is the formal environment.
Related Architecture Design Based on laravel4.2