Laravel - 在服务提供商中需要 php 脚本
Laravel - require php script in a service provider
我是 Laravel 的新手,在 SO 上使用 为我的辅助函数实现了服务提供者。
推荐给:
in the register function of your newly generated
HelperServiceProvider.php add following code
require_once app_path('Helpers/AnythingHelper.php');
但是,Laravel docs声明 register 方法应该只用于将东西绑定到容器中:
As mentioned previously, within the register method, you should only
bind things into the service container. You should never attempt to
register any event listeners, routes, or any other piece of
functionality within the register method.
在我的例子中,应用程序按原样工作,在 register 函数中需要一个语句,所以我的问题更多是关于 'best practices' 而不是让代码工作。
问题:
这是 good/acceptable 方法(注册方法中的 require 语句),还是我应该将 require 语句移动到引导方法?
如果你在这里只放方法(而不是 类),推荐的方法:
- 在任意位置创建文件
在 composer.json
中,确保将此文件添加到 autoload
内的 files
键,如下所示:
"autoload": {
// here other autoload things
"files": ["app/Helpers/AnythingHelper.php"]
},
运行 composer
转储自动加载`
对于 类 显然你应该使用标准的 PSR-4 自动加载
我是 Laravel 的新手,在 SO 上使用
推荐给:
in the register function of your newly generated HelperServiceProvider.php add following code
require_once app_path('Helpers/AnythingHelper.php');
但是,Laravel docs声明 register 方法应该只用于将东西绑定到容器中:
As mentioned previously, within the register method, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method.
在我的例子中,应用程序按原样工作,在 register 函数中需要一个语句,所以我的问题更多是关于 'best practices' 而不是让代码工作。
问题:
这是 good/acceptable 方法(注册方法中的 require 语句),还是我应该将 require 语句移动到引导方法?
如果你在这里只放方法(而不是 类),推荐的方法:
- 在任意位置创建文件
在
composer.json
中,确保将此文件添加到autoload
内的files
键,如下所示:"autoload": { // here other autoload things "files": ["app/Helpers/AnythingHelper.php"] },
运行
composer
转储自动加载`
对于 类 显然你应该使用标准的 PSR-4 自动加载