Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anyone get this working with view composers for each theme? (overlapping view names) #128

Open
michaelsoutherton opened this issue Mar 26, 2020 · 2 comments

Comments

@michaelsoutherton
Copy link

We're trying to use multiple view composers along with this package to help clean up our controllers. The problem is that we can't figure out how to conditionally load only the view composer we want when some of our view names are exactly the same.

Here's a couple different homepages for platforms that each need very different data:

/resources/themes/platform-one-theme/pages/index.blade.php
/resources/themes/platform-two-theme/pages/index.blade.php

The view name for each of those homepages is 'pages.index'

So in our ComposerServiceProvider.php, we have:

View::composer(['pages.index'], 'App\Http\ViewComposers\Platform-One-ViewComposer');
View::composer(['pages.index'], 'App\Http\ViewComposers\Platform-Two-ViewComposer');

...and our problem is that both of those will both be loaded. How can we conditionally apply only the view composer for our theme? It looks like we don't have access to request within ComposerServiceProvider, and it doesn't seem to work with middleware.

@sebsobseb
Copy link

@michaelsoutherton did you found anything? I'm having the same 'problem'..

@michaelsoutherton
Copy link
Author

@michaelsoutherton did you found anything? I'm having the same 'problem'..

Not really. One of our workarounds is currently to append the platform name in the view file, e.g. pages.index-platform1 and pages.index-platform2 and then just create a separate view composers for each index view.

Those examples are homepage views for our different platforms, but a different example would be a 'news' page (similar to a blog archive page). All three of our platforms have a 'news' view. And as you can imagine, they share a lot of the same data (get all the news posts), but they also have their own unique data specific to their platform. What we ended up doing here was using the same view name pages.news.index, adding data for this view in a generic view composer, and then passing $platform (which is based on a key in our route array) along with it and determining what data to add to it in a switch($platform) statement.

Not sure if there's a better way, but that's what we're doing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants