You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel包中如何为所有根视图全局加载View Composer?

Hey there! Let's troubleshoot why your cms::* wildcard isn't applying the NavigationComposer and SiteConfigComposer to your views, and get this sorted out.

First, let's walk through the most common issues and fixes:

1. Double-check your view namespace registration

Make sure your package's service provider is correctly registering the cms view namespace in the register() method, pointing to the right directory:

public function register()
{
    $this->loadViewsFrom(__DIR__.'/../resources/views', 'cms');
}

If your "root views" live directly in resources/views (not nested in subfolders like resources/views/admin), then cms::* should match them. But if you accidentally placed root views in a subdirectory (e.g., resources/views/site), you'd need to adjust the wildcard to match that structure.

2. Ensure you're loading views with the cms:: prefix

This is a super common gotcha! If you're loading root views without the namespace prefix (e.g., view('dashboard') instead of view('cms::dashboard')), the cms::* wildcard won't catch them. Double-check all your view calls to confirm they're using the cms:: namespace for the views you want the composers to apply to.

3. Register composers in the boot() method

View composers need to be registered after the view namespace is set up, so make sure you're adding them in your service provider's boot() method (not register()):

public function boot()
{
    $view = $this->app->make('view');

    // Apply to all cms-namespaced views (root + subdirectories)
    $view->composer('cms::*', NavigationComposer::class);
    $view->composer('cms::*', SiteConfigComposer::class);

    // Your working admin-specific composer
    $view->composer('cms::admin.*', ProfileComposer::class);
}

If you registered composers in register(), the namespace might not exist yet when Laravel processes the wildcard, leading to missed matches.

4. Clear your view cache

Laravel caches view-related configurations, especially in production. Run this command to wipe the cache and force Laravel to pick up your updated composer rules:

php artisan view:clear

This is often the quick fix for "suddenly not working" view composer issues.

5. Verify your composer classes are correctly implemented

Double-check that NavigationComposer and SiteConfigComposer properly implement the compose() method with the right contract:

use Illuminate\Contracts\View\View;

class NavigationComposer
{
    public function compose(View $view)
    {
        // Add your data-passing logic here
        $view->with('navigationItems', /* Your navigation data */);
    }
}

Also, make sure you've imported these classes correctly in your service provider (no typos in namespace or class name!).

6. Want to apply to ALL views (not just cms-namespaced)?

If your goal is to have these composers run for every single view in your application (including non-cms views), swap cms::* with just *:

$view->composer('*', NavigationComposer::class);
$view->composer('*', SiteConfigComposer::class);

This wildcard matches every view, regardless of namespace.

Start with steps 2, 3, and 4—those are the most likely culprits. If none of these work, let me know more details about how your views are structured and loaded, and we can dig deeper!

内容的提问来源于stack exchange,提问作者Friendly Code

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:14:55