Enabling View Composers in Laravel with Composer

I have done below composer viewfor my application. I put it in a separate file in 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);

});

My composer.php file

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ],
    "files": [
        "app/composers.php"
    ]
},

Sorry, I get this error

Fatal error: Class 'View' not found in C:\xampp\htdocs\eshop\app\composers.php on line 15

Update

I also tried this. I wrote inside app / start / global.php

require app_path().'/composers.php';

and

use Illuminate\Support\Facades\View as View ;

in app / composers.php getting this error

Fatal error: call member function () on a non-object in C: \ XAMPP \ HTDOCS \ online store \ vendor \ Laravel \ framework \ SRC \ Light \ Support \ Facades \ Facade.php on line 211

+4
source share
2 answers

As @TheShiftExchange found out, one problem is that you used the file options.

, :

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 simply a convenient wrapper for the registration.
|
*/

$providers = $config['providers'];

$app->getProviderRepository()->load($app, $providers);

classmaps : , , , .

, app/start/global.php :

require app_path() . '/filters.php';

require app_path() . '/composers.php';
require app_path() . '/filters.php';

, , .

+6

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

app/start/global.php, edi9999, .

:

"files": [
    "app/composers.php"
]

2: composer.json!

Laravel docs , - ...

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

/start/global.php:

require app_path() . '/composers.php';

, app/start/global.php, Laravel , .


3: composer.json + ServiceProvider

View Composers Laravel 4, , 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');
    }

}

/Config/app.php:

'providers' => array(
    ...
    'App\Modules\Manager\Composer\ViewComposerServiceProvider',
),
+7

Source: https://habr.com/ru/post/1543828/


All Articles