使用Composer在Laravel中包括View Composer

詹尼斯·克里斯托法基斯

composer view为我的应用做了以下内容我将其放置在app / composers.php的单独文件中

<?php

// namespace App\Modules\Manager\Composer;
// use Illuminate\Support\Facades\View 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:\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()

edi9999

正如@TheShiftExchange所发现的那样,一个问题是您使用了“文件”选项。

如您在作曲家的代码中看到的,autoload部分与此相对应:

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';

这是我想到的在应用程序的每次加载中包含非类文件的最佳方式。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章