Laravel uses composer to load custom functions and custom classes (graphic)

01-28-2024

The content that this article brings to you is about the method (picture and text) that laravel uses composer to load custom functions and custom classes, which has certain reference value. Friends who need it can refer to it and hope to help you. In development, some custom functions and custom classes will be encapsulated. This article talks about how to use composer to realize automatic loading.

Custom function

There are three steps to realize automatic loading.

1. Create a file. Create a Helpers.php file in the app directory to customize the function;

2. Modify the composer.json file and add the following statement

Finally, the composer dump-autoload is executed in the project directory.

Next, you can use the custom function in the code. It should be noted that the custom function should be checked whether it has been defined. For details, please refer to the code in GitHub.

Custom class

Customization is similar, it is also three steps, with some minor changes.

1. The same is to create files, but the difference is to create a Libraries directory under the app, which is convenient for management. You can create a custom class in Libraries. Be careful to add the namespace NamespaceApp \ Libraries;

2. Modify the composer.json file as follows.

Just execute composer dump-autoload.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us