Home> PHP Framework> Laravel> body text

How does Laravel include its own helper functions?

Guanhui
Release: 2020-06-24 18:00:39
forward
2803 people have browsed it

How does Laravel include its own helper functions?

#Many tutorials will say that you can achieve this requirement by adding an automatically loaded file to thecomposer.jsonfile. But I think this is not a good way, when you add more functions in thehelpers.phpfile, the readability will become poor.

Below I will introduce a way that allows you to define many files to contain different functions. This will make our program cleaner and more readable.

Let’s get started

First create a HelperServiceProvider.php service provider file:

php artisan make:provider HelperServiceProvider
Copy after login

Use the above command, you will be inapp \Providersgenerated in the fileHelperServiceProvider.php

You can simply remove theboot()method, we will not use it here.

In theregister()method we add the following code:

public function register(){ foreach (glob(app_path('Helpers') . '/*.php') as $file) { require_once $file; }}
Copy after login

This loop will traverse all the files in theapp/Helpersdirectory, As you may have guessed, now you can create any files in this directory and they will be loaded into your application. These helper functions will be accessible from anywhere in your code (views, models, controllers… )

We also need to load this service provider, openconfig/app.php, and then putHelperServiceProvideron top of yourAppServiceProvider

...App\Providers\HelperServiceProvider::class,App\Providers\AppServiceProvider::class,App\Providers\AuthServiceProvider::class,App\Providers\BroadcastServiceProvider::class,...
Copy after login

Now let us create a simple function and create aCarbon.phpfile in theapp/Helpersdirectory with the following code:


        
Copy after login

You don't need to add any command spaces. If you want, you can usefunction_existsto detect whether this function exists.

Now you can use the helper functioncarbon()anywhere in your application. Now, if you need another function that returns a specific format (just for the use case of this tutorial), you can enter that function in the same file (Carbon.php):

format('Y-m-d')}
Copy after login

Okay! Now you can start populating the app/Helpers directory with your own PHP files containing your frequently used helpers

NOTE: Please keep in mind that I am Dutch and English is not mine My native language, therefore this article may contain some grammatical errors.

Recommended tutorial: "Laravel Tutorial"

The above is the detailed content of How does Laravel include its own helper functions?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!