我有一个我正在修改的自定义包我们在我们公司使用(其他人写了它),我正在实现一个新的视图作曲家。为了使它工作,我正在构建一个从配置文件加载信息的作曲家提供程序。这将允许我们在多个项目中使用此包。
在此提供程序中,我无法从任一配置文件中获取信息。包中的那个,也不是我发布到我的项目中的那个。
正如您将在下面看到的,我尝试了几种方法来获取配置信息。当我将输出打印到屏幕时,我没有看到配置文件的任何内容。我还在提供者下的项目app.php文件中列出了两个提供者。所以我可以将调试代码放入其中,并在屏幕上看到它。
那么,我究竟如何获取配置信息呢?
我尝试通过以下方式访问配置:
$pages = \Config::get('laravel::pages'); //The specific array
$items = \Config::getItems(); //All config info
$config = $this->app['config']; //The more direct approach
此时您可能需要更多来自我的代码以及有关结构的更多信息。
以下是文件夹结构:
/vendor
/bas
/laravel
/src
/BAS
/Laravel
/Provider
/ViewComposerProvider.php
/LaravelServiceProvider.php
/config
/config.php
ViewComposerProvider.php的内容:
<?php
namespace BAS\Laravel\Provider;
use Illuminate\Support\ServiceProvider;
class ViewComposerProvider extends ServiceProvider
{
public function register()
{
/**
* This has to be declared as a singleton due to the wildcard view
* selector in the related boot function.
*
* If it were not a singleton then we would hit ldb for company
* data once for every view and subview that is built.
*/
\App::singleton(
'BAS\Laravel\Composers\ICompanyComposer',
'BAS\Laravel\Composers\CompanyComposer'
);
\App::singleton(
'BAS\Laravel\Composers\IJavascriptLoggerComposer',
'BAS\Laravel\Composers\JavascriptLoggerComposer'
);
}
public function boot()
{
/**
* Default for the array looks like this:
*
* array(
* 'BAS\Laravel\Composers\ICompanyComposer' => array('company::pages.*'),
* 'BAS\Laravel\Composers\IJavascriptLoggerComposer' => array('company::base')
* );
*/
\View::composers( $this->getComposers() );
}
public function getComposers()
{
$app = $this->app;
/**
* This package is called Laravel, so this could get confusing.
*
* @var Array $pages
*/
$pages = \Config::get('laravel::pages');
$composerList = array();
/*echo "Pages:<pre>" . print_r($pages, true) . "</pre>";
$items = \Config::getItems();
echo "Items:<pre>" . print_r($items, true) . "</pre>";
$config = $app['config'];
echo "Config:<pre>" . print_r($config, true) . "</pre>";
die();*/
foreach ($pages as $pageType => $pageList) {
$composer = $this->getComposerByPageType($pageType);
$composerList[$composer] = $pageList;
}
return $composerList;
}
public function getComposerByPageType($type)
{
$composer = '';
switch ( strtolower($type) ) {
case 'company':
$composer = 'BAS\Laravel\Composers\ICompanyComposer';
break;
case 'javascript':
$composer = 'BAS\Laravel\Composers\IJavascriptLoggerComposer';
break;
}
return $composer;
}
}
以下是LaravelServiceProvider.php的内容:
<?php
namespace BAS\Laravel\Provider;
use Illuminate\Support\ServiceProvider;
class LaravelServiceProvider extends ServiceProvider
{
protected $defer = false;
public function boot()
{
//$this->package('bas/laravel');
}
public function register()
{
$this->package('bas/laravel');
}
public function provides()
{
return array();
}
}
最后是配置文件的内容:
<?php
return array(
'pages' => array(
'company' => array('company::pages.*', 'card-process'),
'javascript' => array('company::base')
),
);