我composer view
为我的应用程序制作了以下内容.我把它放在app/composers.php的单独文件中.
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:\xampp\htdocs\eshop\app\composers.php on line 15
我也尝试过这个.我在app/start/global.php里面写道
require app_path().'/composers.php';
和
use Illuminate\Support\Facades\View as View ;
在app/composers.php,收到此错误
致命错误:在第211行的C:\ xampp\htdocs\eshop\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php中的非对象上调用成员函数composer()
Unnawut.. 7
我不认为你app/composers.php
应该在作曲家中自动加载.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';
在edi9999之app/start/global.php
类的底部说会没事的.
在这种情况下不要忘记删除:
"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.json中自动加载类+注册一个自定义的ServiceProvider
学习使用 Philip Brown 在Laravel 4中使用View Composers,我们还可以添加自己的自定义服务提供程序,而无需编辑我们的app/start/global.php
文件.
应用程序/ viewcomposers/HeaderViewComposer.php:
withCategories( $categories); } }
composer.json:
"classmap": [ ... "app/viewcomposers" ]
应用程序/ viewcomposers/ViewComposerServiceProvider.php:
app->view->composer('tshop.includes.header', 'App\Modules\Manager\Composer\HeaderViewComposer'); } }
应用程序/配置/ app.php:
'providers' => array( ... 'App\Modules\Manager\Composer\ViewComposerServiceProvider', ),
edi9999.. 6
正如@TheShiftExchange发现的那样,一个问题是您使用了"文件"选项.
正如您在作曲家的代码中看到的那样,自动加载部分对应于:
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之前,在编辑器的自动加载过程中需要您指定的文件数组.
外墙的供应商已加载 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);
实际上,类映射的问题是另一个问题:它们在文件中不是类,因此永远不会加载文件,因此它不会执行任何操作.
要使其工作,您应该添加到app/start/global.php
文件的末尾:
代替
require app_path() . '/filters.php';
写
require app_path() . '/composers.php'; require app_path() . '/filters.php';
这是我能想到的最好的方法,即在应用程序的每个负载中包含非类的文件.
正如@TheShiftExchange发现的那样,一个问题是您使用了"文件"选项.
正如您在作曲家的代码中看到的那样,自动加载部分对应于:
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之前,在编辑器的自动加载过程中需要您指定的文件数组.
外墙的供应商已加载 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);
实际上,类映射的问题是另一个问题:它们在文件中不是类,因此永远不会加载文件,因此它不会执行任何操作.
要使其工作,您应该添加到app/start/global.php
文件的末尾:
代替
require app_path() . '/filters.php';
写
require app_path() . '/composers.php'; require app_path() . '/filters.php';
这是我能想到的最好的方法,即在应用程序的每个负载中包含非类的文件.
我不认为你app/composers.php
应该在作曲家中自动加载.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';
在edi9999之app/start/global.php
类的底部说会没事的.
在这种情况下不要忘记删除:
"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.json中自动加载类+注册一个自定义的ServiceProvider
学习使用 Philip Brown 在Laravel 4中使用View Composers,我们还可以添加自己的自定义服务提供程序,而无需编辑我们的app/start/global.php
文件.
应用程序/ viewcomposers/HeaderViewComposer.php:
<?php namespace App\Modules\Manager\Composer; class HeaderViewComposer { public function compose($view) { $categories = Categories::getWithChilds(); $view->withCategories( $categories); } }
composer.json:
"classmap": [ ... "app/viewcomposers" ]
应用程序/ viewcomposers/ViewComposerServiceProvider.php:
<?php namespace App\Modules\Manager\Composer; use Illuminate\Support\ServiceProvider; class ViewComposerServiceProvider extends ServiceProvider { public function register() { $this->app->view->composer('tshop.includes.header', 'App\Modules\Manager\Composer\HeaderViewComposer'); } }
应用程序/配置/ app.php:
'providers' => array( ... 'App\Modules\Manager\Composer\ViewComposerServiceProvider', ),