Laravel Create a global function _php technique similar to the functions.php in thinphp

Source: Internet
Author: User
Tags autoload require

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.

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.