Objective
It has always been felt that the public functions in thinphp are a good design, because we only need to encapsulate the shared functions in the functions.php, and then we can call them directly on the global. In fact, Laravel also have similar functions, such as helper functions, similar to the session()
functions, these helper functions are also global can be called, very convenient.
Here's a summary of the differences and similarities between the two.
TP3 Series functions.php file default is actually empty file, very easy to find. We can encapsulate the code directly.
In the Laravel5 series path/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
. The encapsulated function in this file is a global function that can be invoked in any region.
However, many of the built-in helper functions are already encapsulated in the helper.php in Laravel, and it is generally not recommended that you encapsulate the functions directly in the file, but instead create a new file with a function that needs to be encapsulated.
Here is a point to note: the implementation of the global function is dependent on the initialization, the helps.php or functions.php directly loaded. In Laravel, path/bootstrap/autoload.php
you can define what files to load at initialization time.
By default, there is one of the following load configurations:
Require __dir__. ' /.. /vendor/autoload.php ';
Similarly, we can load other files, such as the following loading operations:
Require __dir__. ' /.. /app/common/functions.php ';
In this way, we can directly encapsulate the required functions in this loaded file, which is what we need.
In addition, another method is recommended, that is, the use of a controller, in the controller of the class to encapsulate static methods, where necessary, to introduce the required controller, can also achieve our needs.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone to learn or use Laravel can have some help, if there is doubt you can message exchange.