PHPackages                             monospice/laravel-view-composers - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. monospice/laravel-view-composers

ActiveLibrary[Templating &amp; Views](/categories/templating)

monospice/laravel-view-composers
================================

An intuitive abstraction for organizing Laravel View Composers and View Creators

1.0.1(10y ago)4118MITPHPPHP &gt;=5.3.0

Since Oct 1Pushed 10y ago1 watchersCompare

[ Source](https://github.com/monospice/laravel-view-composers)[ Packagist](https://packagist.org/packages/monospice/laravel-view-composers)[ RSS](/packages/monospice-laravel-view-composers/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (3)Used By (0)

Laravel View Composers
======================

[](#laravel-view-composers)

[![Build Status](https://camo.githubusercontent.com/a38aabd6f12631ba45299d16ccb5d80563235ea431a83f842094797ab56b5086/68747470733a2f2f7472617669732d63692e6f72672f6d6f6e6f73706963652f6c61726176656c2d766965772d636f6d706f736572732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/monospice/laravel-view-composers)

**An intuitive abstraction for organizing Laravel View Composers and View Creators.**

View Composers in Laravel improve application structure by consolidating the controller-independent data-binding logic for a view.

This package provides a readable boilerplate framework to easily define the View Composer and View Creator bindings in your Laravel application.

Compatible with Laravel 4 and 5+. For more information about View Composers and View Creators, see the [Laravel Documentation](http://laravel.com/docs/5.1/views#view-composers).

Simple Example
--------------

[](#simple-example)

In the following example, the application will use `MyViewComposer` to compose `myview`, `UserComposer` to compose the `user.profile` and `user.image` views, and both `UserComposer` and `FavoritesComposer` to compose the `user.favorites`view.

```
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!
}
```

Installation
------------

[](#installation)

Simply install via composer:

```
$ composer require monospice/laravel-view-composers
```

### Create the Service Provider

[](#create-the-service-provider)

The Service Provider takes care of the view binding work when the application boots. Simply extend the Service Provider in this package:

```
use Monospice\LaravelViewComposers\ViewBinderServiceProvider;

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

And don't forget to add the new Service Provider to `app.config`:

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

No need to declare the `register()` or `boot()` methods. The package's service provider takes care of this.

Binding Views
-------------

[](#binding-views)

Define View Composer and View Creator bindings in the Service Provider you created during installation.

Definitions must be placed inside a method that begins with "bind" and ends with "Views", such as `bindViews()` or `bindAnythingGoesHereViews()`. This convention encourages readable groups of related view bindings:

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

### Namespaces

[](#namespaces)

To make these definitions more concise, use the `setNamespace()` method to declare the namespace to use for the following View Composer or View Creator classes.

```
...
    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');
    }
...
```

In the example above, the Service Provider applies the `App\Http\ViewComposers`namespace to both the `CommentComposer` and the `AnotherComposer` classes.

One may change the namespace at any time by calling `setNamespace()` again. Any namespaces are automatically cleared at the end of each `bindViews()`method.

### View Prefixes

[](#view-prefixes)

Similar to namespaces above, one may set the namespace-like prefix of the bound views by calling `setPrefix()` for more concise code:

```
...
    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');
    }
...
```

As demonstrated, the application binds the `partials.navbar.info.user` and `partials.navbar.info.company` views to the `NavbarComposer`.

One may change the prefix at any time by calling `setPrefix()` again. Any prefixes are automatically cleared at the end of each `bindViews()` method.

### View Composers

[](#view-composers)

Use the `compose()` method to specify the views that the application should bind to a particular View Composer, and `with()` to specify which View Composer to use. The View Composer specified in `with()` may be a class name or an anonymous function, as described in the [Laravel Docs](http://laravel.com/docs/5.1/views#view-composers):

```
...
    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
            });
    }
...
```

### View Creators

[](#view-creators)

Similar to View Composers, use the `create()` method to specify the views that the application should bind to a particular View Creator.

```
...
    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
            });
    }
...
```

Testing
-------

[](#testing)

The Laravel View Composers package uses PHPSpec to test object behavior:

```
$ vendor/bin/phpspec run
```

License
-------

[](#license)

The MIT License (MIT). Please see the [LICENSE File](LICENSE) for more information.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3882d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c752b346dd0bb5050bfcceb1883de54f71cc37fa94c0f215ce35d215e963bdf5?d=identicon)[cyrossignol](/maintainers/cyrossignol)

---

Top Contributors

[![cyrossignol](https://avatars.githubusercontent.com/u/4282384?v=4)](https://github.com/cyrossignol "cyrossignol (10 commits)")

---

Tags

composerlaravelviewcreator

### Embed Badge

![Health badge](/badges/monospice-laravel-view-composers/health.svg)

```
[![Health](https://phpackages.com/badges/monospice-laravel-view-composers/health.svg)](https://phpackages.com/packages/monospice-laravel-view-composers)
```

###  Alternatives

[ambengers/kinetic

Adds view-composer like feature to Inertia.js Laravel adapter.

9058.0k1](/packages/ambengers-kinetic)[torann/device-view

Provides support for device based view layouts in Laravel.

146.6k](/packages/torann-device-view)[delatbabel/viewpages

Support rendering/view of Laravel pages and templates from a database.

121.4k](/packages/delatbabel-viewpages)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
