PHP code example of monospice / laravel-view-composers

1. Go to this page and download the library: Download monospice/laravel-view-composers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

monospice / laravel-view-composers example snippets


class ViewComposerServiceProvider extends ViewBinderServiceProvider
{
    protected function bindViews()
    {
        $this->compose('myview')->with('MyViewComposer');
    }

    protected function bindUserViews()
    {
        $this->setPrefix('user')
            ->compose('profile', 'image')->with('UserComposer')
            ->compose('favorites')->with('UserComposer', 'FavoritesComposer');
    }

    // ...and so on!
}

use Monospice\LaravelViewComposers\ViewBinderServiceProvider;

class ViewComposerServiceProvider extends ViewBinderServiceProvider
{
    // View Composer and View Creator bindings will go here
}

...
    // Laravel >= 5.1:
    App\Providers\ViewComposerServiceProvider::class,
    // Laravel < 5.1:
    'App\Providers\ViewComposerServiceProvider',
...

class ViewComposerServiceProvider extends ViewBinderServiceProvider
{
    protected function bindCommentViews()
    {
        // all comment-related view bindings go here
    }
}

...
    protected function bindCommentViews()
    {
        // The hard way
        $this->compose('view')->with('App\Http\ViewComposers\CommentComposer');

        // or just:
        $this->setNamespace('App\Http\ViewComposers')
            ->compose('view2')->with('CommentComposer')
            ->compose('view3')->with('AnotherComposer');
    }
...

...
    protected function bindNavbarViews()
    {
        // The hard way
        $this->compose('partials.navbar.info.user')->with('NavbarComposer');

        // or just:
        $this->setPrefix('partials.navbar.info')
            ->compose('user', 'company')->with('NavbarComposer');
    }
...

...
    protected function bindProductViews()
    {
        $this->setNamespace('App\Http\ViewComposers')->setPrefix('product');

        $this
            ->compose('index', 'search')->with('ProductComposer')
            ->compose('show')->with(function ($view) {
                // view composer logic here
            });
    }
...

...
    protected function bindStudentViews()
    {
        $this->setNamespace('App\Http\ViewCreators')->setPrefix('dashboard');

        $this
            ->create('student', 'teacher')->with('DashboardCreator')
            ->create('feed')->with(function ($view) {
                // view creater logic here
            });
    }
...