找不到 Laravel 5.2 自定义包路由


Laravel 5.2 custom package route not found

我按照文档创建了一个自定义包。(https://laravel.com/docs/5.2/packages#routing)

我在自定义路由文件中添加了一个路由,它在检查"php artisan route:list"时出现,但我得到 NotFoundHttpException。

我使用"php artisan route:clear"来清除路由缓存,但问题仍然存在。

如果我在主应用程序路由文件中添加路由,则可以正常工作。

服务提供商:

/**
 * Bootstrap the application services.
 *
 * @return void
 */
public function boot()
{
    $this->publishes([
        __DIR__ . '/../database/migrations/' => database_path('migrations')
    ], 'migrations');
    if (! $this->app->routesAreCached()) {
        require __DIR__.'/../Http/routes.php';
    }
}

自定义路由文件:

Route::get('foo', function () {
    return 'Hello World';
});

对于您在这里提供的信息,我不能给出明确的答案。

如果你的包结构是这样的

PackageMainDir
  - src
    - Controllers
    - Migrations
    - Models
    PackageServiceProvider.php
    Routes.php
  - test
  - vendor
  composer.json
  phpunit.xml

您将在 PackageServiceProvider 中.php具有以下代码:

/**
 * Bootstrap the application services.
 *
 * @return void
 */
public function boot()
{
    $this->publishes([
        realpath(__DIR__.'/Migrations') => $this->getDatabasePath().'/migrations',
    ]);
    if (! $this->app->routesAreCached()) {
        require_once(__DIR__ . '/Routes.php');
    }
}

我希望它不会迟到,否则其他用户将从我的答案中获益。

解决了,我不知道为什么,但是在尝试php artisan config:cache之后,考虑了配置/app.php中的更改,并且自定义服务提供商加载了boot()方法。

经过

一些研究并解决了它,我也有同样的经历! 请在包服务提供程序文件中编辑您的代码 function boot() .

public function boot()
    {
        if (! $this->app->routesAreCached()) {
            require __DIR__ . '/Routes.php';
        }
    }

在此运行 cmd 中的php artisan config:cache代码后,我解决了这个问题。

我有同样的问题,我用这种方式解决了它

public function boot()
{
    $path = realpath(__DIR__.'/../');
    $this->loadRoutesFrom($path.'/Routes/web.php');
    $this->loadViewsFrom($path.'/Resources/views/Formato','formato');
}
public function register()
{
}

我希望这可以有用