在auth.php文件my laravel包中添加新的Guard
为了学习,我开始创建AdminLoginLaravel包,在这个包中,我需要从我的包中向auth.php文件添加卫士和提供者 从Laravel文档中,我知道我们可以将配置文件添加到 服务提供程序启动方法中的项目配置文件夹,如下所示在auth.php文件my laravel包中添加新的Guard,php,laravel,packages,laravel-5.6,Php,Laravel,Packages,Laravel 5.6,为了学习,我开始创建AdminLoginLaravel包,在这个包中,我需要从我的包中向auth.php文件添加卫士和提供者 从Laravel文档中,我知道我们可以将配置文件添加到 服务提供程序启动方法中的项目配置文件夹,如下所示 public function boot() { $this->publishes([ __DIR__.'/path/to/config/courier.php' => config_path('courier.php'),
public function boot()
{
$this->publishes([
__DIR__.'/path/to/config/courier.php' => config_path('courier.php'),
]);
}
但是如何添加我的配置文件以与config/auth.php文件合并
下面几行代码将做什么,我不是从laravel文档中得到的
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/path/to/config/courier.php', 'courier'
);
}
下面是我的自定义服务提供商代码
<?php
namespace Abhilash\AdminLogin;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Arr;
class AdminLoginServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__.'/config/admin.php' => config_path('auth.php'),
]);
}
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/config/admin.php', 'auth'
);
}
/**
* Merge the given configuration with the existing configuration.
*
* @param string $path
* @param string $key
* @return void
*/
protected function mergeConfigFrom($path, $key)
{
$config = $this->app['config']->get($key, []);
$this->app['config']->set($key, $this->mergeConfigs(require $path, $config));
}
/**
* Merges the configs together and takes multi-dimensional arrays into account.
*
* @param array $original
* @param array $merging
* @return array
*/
protected function mergeConfigs(array $original, array $merging)
{
$array = array_merge($original, $merging);
foreach ($original as $key => $value) {
if (! is_array($value)) {
continue;
}
if (! Arr::exists($merging, $key)) {
continue;
}
if (is_numeric($key)) {
continue;
}
$array[$key] = $this->mergeConfigs($value, $merging[$key]);
}
return $array;
}
}
我不会合并配置,因为它不会合并多维数组,并且根据这些配置的内容或复杂性,它可能会变得混乱。总之,它将把$key
的内容与配置的$value
合并。但您可以做的是在服务提供商
中覆盖合并配置自
,或者您可以创建一个新函数并在注册表上使用它,如下所示:
/**
* Merges the configs together and takes multi-dimensional arrays into account.
*
* @param array $original
* @param array $merging
* @return array
*/
protected function mergeConfigs(array $original, array $merging) {
$array = array_merge($original, $merging);
foreach ($original as $key => $value) {
if (! is_array($value)) {
continue;
}
if (! Arr::exists($merging, $key)) {
continue;
}
if (is_numeric($key)) {
continue;
}
$array[$key] = $this->mergeConfig($value, $merging[$key]);
}
return $array;
}
免责声明:我不是作者,但我使用过此函数一次,它可以正常工作,这对我不起作用,何时合并配置,我是否需要运行任何其他命令。php artisan供应商:发布并告诉我出了什么问题。当我运行php artisan vendor:publish no changes in auth.php时,我通过添加服务提供商代码编辑了我的问题,结果是什么file@Abhilash.k.p不要怀疑您正在实时修改配置,而不是更新原始身份验证文件。