Context API — Overview
Inspired by WordPress template tags, the Context API provides typed wrapper objects automatically injected into the right views. Theme developers work with clean method calls — never raw Eloquent models or array keys.
ℹ️ ℹ How injection works BlogComposer is registered for all blog::* views and ShopComposer for all theme::frontend.shop.* views. They run automatically and inject $theme_* Blade variables. You can also call the equivalent helper functions anywhere.
⚠️ ⚠ Provider boot order — blog sidebar data ThemeServiceProvider (an app provider) boots before BlogServiceProvider (a module provider), so BlogComposer runs before the module's own blog::* view composer. As a result, BlogComposer does not rely on $data['all_category'] being passed by the controller. It self-fetches BlogCategory and BlogTag directly from the database using a !View::shared() guard to avoid duplicate queries. Implication: theme_blog_categories() and theme_blog_tags() always return data on every blog::* view with no controller changes. If sidebar data is missing, the fix belongs in BlogComposer, not in individual controllers.
Two ways to access the same data:
// Option A — Blade variable (injected by composer)
@foreach($theme_blogs as $post)
{{ $post->title() }}
@endforeach
// Option B — Helper function (works anywhere, even in partials)
@foreach(theme_blogs() as $post)
{{ $post->title() }}
@endforeach
Blog Context
Available in all blog::* views (list, single, category, search).
| Variable / Function | Type | Available in | Description |
|---|---|---|---|
$theme_blogs / theme_blogs() | Collection<Blog\Post> | blog-all, blog-category, blog-search | Current page posts as Post wrappers |
$theme_blogs_paginator / theme_blogs_paginator() | LengthAwarePaginator | blog-all, blog-category | Raw paginator for ->links() |
$theme_post / theme_post() | Blog\Post | blog-single | Current post wrapper |
$theme_comments / theme_comments() | Collection<Blog\Comment> | blog-single | Top-level comments |
$theme_comments_count / theme_comments_count() | int | blog-single | Total comment count |
$theme_blog_categories / theme_blog_categories() | Collection<Blog\Category> | all blog views | Sidebar categories |
$theme_blog_tags / theme_blog_tags() | Collection<Blog\Tag> | all blog views | Sidebar tags |
Shop Context
Available in all theme::frontend.shop.* views.
| Variable / Function | Type | Available in | Description |
|---|---|---|---|
$theme_products / theme_products() | Collection<Shop\Product> | shop listing | Current page products |
$theme_product / theme_product() | Shop\Product | product detail | Current product wrapper |
$theme_related_products / theme_related_products() | Collection<Shop\Product> | product detail | Related products |
$theme_shop_categories / theme_shop_categories() | Collection<Shop\ProductCategory> | shop listing | Sidebar categories |

