我已经为我的应用程序做了下面的composer view
。我把它放在单独的文件app/composer .php。
<?php
// namespace AppModulesManagerComposer;
// use IlluminateSupportFacadesView as View ;
/*
|--------------------------------------------------------------------------
| Composers
|--------------------------------------------------------------------------
|
|
*/
View::composer('tshop.includes.header', function($view)
{
$categories = Categories::getWithChilds();
$view->withCategories( $categories);
});
我的composer.php文件是
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"files": [
"app/composers.php"
]
},
不幸的是,我得到了这个错误
Fatal error: Class 'View' not found in C:xampphtdocseshopappcomposers.php on line 15
更新我也试过这个。我写在app/start/global.php
require app_path().'/composers.php';
和
use IlluminateSupportFacadesView as View ;
at app/composer .php,得到这个错误
致命错误:在非对象上调用成员函数composer()C: xampp 根 eshop 供应商 laravel framework src 照亮 外墙 Facade.php支持第211行
我不认为你的app/composers.php
应该在composer中自动加载。Composer的职责是解析包并为您安装它们,这与应用程序逻辑无关,更不用说应用程序的视图了。
在运行composer的时候,它不会知道你的Laravel应用程序。这意味着你的Laravel外观,如View
, Input
, DB
, Auth
等还没有加载。因此你的代码抛出Call to a member function composer() on a non-object
.
方法1:
Laravel没有严格指定你把Laravel视图作曲家放在哪里,所以需要添加:
require app_path() . '/composers.php';
在app/start/global.php
的底部,就像edi9999说的会很好。
在这种情况下不要忘记删除:
"files": [
"app/composers.php"
]
方法2:有一种方法可以在composer.json中自动加载你的视图作曲家!
从Laravel文档中关于视图作曲家的例子来看,你可以这样做…
应用程序/viewcomposers/HeaderViewComposer.php:
class HeaderViewComposer
{
public function compose($view)
{
$categories = Categories::getWithChilds();
$view->withCategories( $categories);
}
}
composer.json:
"classmap": [
...
"app/viewcomposers"
]
应用程序/composers.php:
View::composer('tshop.includes.header', 'HeaderViewComposer');
app/start/global.php底部:
require app_path() . '/composers.php';
不幸的是,您仍然需要将上面的行添加到app/start/global.php
中,以便Laravel知道定义了哪些视图作曲家。
方法3:在composer中自动加载类。注册自定义ServiceProvider
从Philip Brown在Laravel 4中使用视图作曲家学习,我们也可以添加我们自己的自定义服务提供商,而不必编辑我们的app/start/global.php
文件。
应用程序/viewcomposers/HeaderViewComposer.php:
<?php namespace AppModulesManagerComposer;
class HeaderViewComposer
{
public function compose($view)
{
$categories = Categories::getWithChilds();
$view->withCategories( $categories);
}
}
composer.json:
"classmap": [
...
"app/viewcomposers"
]
应用程序/viewcomposers/ViewComposerServiceProvider.php:
<?php namespace AppModulesManagerComposer;
use IlluminateSupportServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
public function register()
{
$this->app->view->composer('tshop.includes.header', 'AppModulesManagerComposerHeaderViewComposer');
}
}
app/config/app.php:
'providers' => array(
...
'AppModulesManagerComposerViewComposerServiceProvider',
),
@TheShiftExchange发现,一个问题是您使用了"files"选项。
正如你在composer的代码中看到的,自动加载部分对应如下:class ComposerAutoloaderInitf8489489s7f894ds98f47d
{
....
....
public static function getLoader()
{
....
....
$includeFiles = require __DIR__ . '/autoload_files.php';
foreach ($includeFiles as $file) {
composerRequiref4s65f4556sd4f564fsdfd($file);
}
return $loader;
}
}
function composerRequire5894s89f4sd98498489f7b37d($file)
{
require $file;
}
所以你指定的文件数组在编译器的自动加载过程中是必需的,远在View Facade被加载之前。
facade的提供程序在vendor/laravel/framework/illuminate/foundation/start.php
/*
|--------------------------------------------------------------------------
| Register The Core Service Providers
|--------------------------------------------------------------------------
|
| The Illuminate core service providers register all of the core pieces
| of the Illuminate framework including session, caching, encryption
| and more. It's simply a convenient wrapper for the registration.
|
*/
$providers = $config['providers'];
$app->getProviderRepository()->load($app, $providers);
实际上,classmap的问题是另一个:它不是你文件中的类,所以文件永远不会被加载,所以它不做任何事情。
要使其工作,您应该在文件末尾的app/start/global.php
中添加:
不是require app_path() . '/filters.php';
写require app_path() . '/composers.php';
require app_path() . '/filters.php';
这是我能想到的在每次加载应用程序时包含非类文件的最好方法。