简体   繁体   English

在Laravel中初始化助手类

[英]Initialize helper class in Laravel

I wrote a simple helper class, placed it in app\\Helpers\\MyHelper.php and created an alias for it in the app.php configuration file: 我编写了一个简单的帮助程序类,将其放在app\\Helpers\\MyHelper.php并在app.php配置文件中app\\Helpers\\MyHelper.php创建了别名:

    'MyHelper' => App\Helpers\MyHelper::class,

The problem is that, now I need to use the class constructor to initialize some default values in it. 问题是,现在我需要使用类构造函数在其中初始化一些默认值。 However, the __construct function will never get called. 但是, __construct函数将永远不会被调用。 Obviously, since it never gets initialized. 显然,因为它从未被初始化。

The question is, how (or where) can I initialize the helper class properly, so it could initialize it's default values? 问题是,如何(或在何处)正确地初始化helper类,以便可以初始化其默认值?

My first thought: AppServiceProvider.php file and the boot() function. 我的第一个想法: AppServiceProvider.php文件和boot()函数。

I have placed a new MyHelper(); 我已经放置了一个new MyHelper(); line there and it's working, however I'm wondering if this is a proper way doing it? 行在那里,它正在工作,但是我想知道这是否是正确的方法吗?

You can create helper class has static class so there no need to create instance of the class for example 您可以创建具有静态类的帮助器类,因此无需创建该类的实例

<?php

namespace App\Helpers;

class HelperFunction {

    public static function createGroup() {

}

so you can import class it in your controller using use keyword and access like 因此,您可以使用use关键字在类中将其导入到控制器中,并像

HelperFunction::createGroup();

In general Services Providers are there for bootstrapping your application. 通常,服务提供者可以引导您的应用程序。 The documentation doesn't say much about the AppServiceProvider but it seems to be there to bootstrap your app in general. 该文档并未对AppServiceProvider过多说明,但似乎可以从总体上引导您的应用程序。

If you want to go the safe route you can create a new Service Provider by using php artisan make:provider MyHelperServiceProvider and add it in the providers array in your config/app.php file. 如果您想走安全路线,则可以使用php artisan make:provider MyHelperServiceProvider创建新的服务提供商,并将其添加到config/app.php文件的providers数组中。

It looks identical to the AppServiceProvider but depending on your current code base it could be cleaner to put that helper into its own Service Provider. 它看起来与AppServiceProvider相同,但是根据您当前的代码库,将帮助程序放入其自己的服务提供程序中可能会更干净。

I'm also just a beginner but to me both seem to be fine. 我也是一个初学者,但对我来说似乎都不错。

update: okay, found the important part in the docs: https://laravel.com/docs/5.5/lifecycle 更新:好的,在文档中找到了重要的部分: https : //laravel.com/docs/5.5/lifecycle

The last paragraph says: 最后一段说:

By default, the AppServiceProvider is fairly empty. 默认情况下, AppServiceProvider相当空。 This provider is a great place to add your application's own bootstrapping and service container bindings. 该提供程序是添加应用程序自己的引导程序和服务容器绑定的好地方。 Of course, for large applications, you may wish to create several service providers, each with a more granular type of bootstrapping. 当然,对于大型应用程序,您可能希望创建多个服务提供者,每个服务提供者都具有更精细的引导类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM