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

Laravel自定义包中如何通过路由访问LicenseExpired视图

自定义Laravel授权包视图访问问题排查

问题描述

开发团队项目授权管理的自定义Laravel包时,其余功能均正常,但无法通过路由访问LicenseExpired视图。

包结构说明

包内包含Migrations(迁移文件目录)、Routes(路由目录)、Views(视图目录),以及核心服务提供者LicenseServiceProvider。

路由文件代码(Routes/index.php)

Route::get('/activate-product', function () {
    return view('LicenseExpired');
})->name('license');

Route::post('/activate-product', [Index::class, 'webStore'])->name('license.store');

Route::post('api/license', [Index::class, 'store']);

LicenseServiceProvider的boot方法代码

public function boot()
{
    // Load migrations
    $this->loadMigrationsFrom(__DIR__.'/Migrations');

    // Load views
    $this->loadViewsFrom(__DIR__.'/Views', 'Lamine/License');

    // Load Routes
    $this->loadRoutesFrom(__DIR__ . '/Routes/index.php');

    $this->publishes([
        __DIR__.'/Migrations' => database_path('migrations'),
    ], 'migrations');

    // publish routes
    $this->publishes([
        __DIR__.'/Routes/index.php' => base_path('routes/license.php'),
    ], 'routes');

    // publish views
    $this->publishes([
        __DIR__.'/Views' => resource_path('views'),
    ], 'views');
}

解决方案

1. 修正视图引用的命名空间

在服务提供者中,你给视图设置了命名空间Lamine/License,路由中引用视图时必须加上该命名空间前缀:

Route::get('/activate-product', function () {
    return view('Lamine/License::LicenseExpired');
})->name('license');

如果不添加命名空间,Laravel会默认在项目的resource/views目录下查找视图,而非包内的Views目录。

2. 确认视图文件存在

检查包内Views目录下是否存在LicenseExpired.blade.php文件,注意文件名大小写需和引用完全一致(Laravel视图文件名区分大小写)。

3. 发布视图(可选)

若已执行php artisan vendor:publish --tag=views将包内视图发布到项目resource/views目录,可直接使用view('LicenseExpired'),但需确保发布后的视图文件路径正确。

内容的提问来源于stack exchange,提问作者Omar Lamin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 19:44:58