Reputation: 3578
I'm building a page builder in laravel. The frontend renders "blocks" that are added to a page in my admin. I figured that the best way to make sure these blocks are available everywhere I need them is to load them in a View Composer which works great;
View::composer('*', function ($view) {
$blocks = Page::where('url', url()->current());
return view()->with(['blocks', $blocks]);
});
However, this attempts to load blocks for frontend routes and for admin routes. Is there any way to make sure they're only loaded for routes on the frontend?
I've split my routes in to separate files like this;
// lets me use an admin.php for my admin routes, instead of web.php
$this->mapAdminRoutes();
protected function mapAdminRoutes()
{
Route::prefix('admin')
->middleware('admin')
->namespace($this->namespace)
->group(base_path('routes/admin.php'));
}
I did find this question when googling
but that still loads the service provider on all routes - just defers the view composer (which works for that OPs performance concerns).
Upvotes: 0
Views: 2151
Reputation: 14278
A middleware is better for this type of things, as you can apply it to whichever route/group of routes you want.
Upvotes: 1