Laravel view composers for different themes that use the same view names are overwriting each other

Viewed 382

We're utilizing view composers to help clean up our controllers. We're also using multiple themes (igaster/laravel-theme). The problem we're running into with view composers is that we have some pages with the same view name, so the wrong view composer is applied.

Here are two separate homepages with very different data (because they're two completely different platforms):

  • /resources/themes/platform-1-theme/pages/index.blade.php
  • /resources/themes/platform-2-theme/pages/index.blade.php

So they both share the view name: 'pages.index'

Our ComposerServiceProvider.php looks something like this:

public function boot()
{
    View::composer(['pages.index'], 'App\Http\ViewComposers\Platform-1-ViewComposer');
    View::composer(['pages.index'], 'App\Http\ViewComposers\Platform-2-ViewComposer');
}

Obviously that's a problem because both composers will be loaded for the homepage even though they're two different platforms. Is there a way to conditionally load only the one we want? As far as we can tell, middleware doesn't seem to work with view composers.


edit:

As requested, here's the directory structure of views folder:

  • resources/
    • views/
      • inc/
        • breadcrumbs.blade.php
        • comments.blade.php
        • login-form.blade.php
        • message.blade.php
      • pages/
        • admin/
        • account/
    • themes/
      • _parents/
        • platform1-parent-theme/
          • inc/
            • footer.blade.php
            • header.blade.php
          • pages/
            • index.blade.php
            • news.blade.php
            • itinerary.blade.php
        • platform2-parent-theme/
          • inc/
            • footer.blade.php
            • header.blade.php
          • pages/
            • index.blade.php
            • news.blade.php
            • points.blade.php
      • client1/
        • client1-platform1-child-theme/
          • inc/
            • header.blade.php
          • pages/
        • client1-platform2-child-theme/
          • inc/
            • header.blade.php
          • pages/
      • client2/
        • client2-platform1-child-theme/
          • inc/
            • header.blade.php
          • pages/
            • custompage.blade.php
        • client2-platform2-child-theme/
          • inc/
            • header.blade.php
          • pages/
            • anothercustompage.blade.php

My client folders extend the parent theme, and the /resources/views/ folder is for the global blade templates used across all platforms. So, Laravel currently looks first in child themes for a view, and if it doesn't find it there it looks in its parent theme, and if it still doesn't find it there then it looks last in the /resources/views/ folder.

1 Answers

As you have different views with different data, it makes sense to have several View Composers. Sharing the same name doesn't mean that they need to share the same composer, you can register a different one for each.

Related