【问题标题】:Laravel 5.2 custom package route not found未找到 Laravel 5.2 自定义包路径
【发布时间】:2016-06-28 12:13:48
【问题描述】:

我按照文档创建了一个自定义包。 (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';
});

【问题讨论】:

  • 我也有同样的问题,你找到解决办法了吗?

标签: php model-view-controller laravel-routing laravel-5.2


【解决方案1】:

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

如果你的包结构是这样的

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');
    }
}

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

【讨论】:

    【解决方案2】:

    解决了,不知道为什么,尝试php artisan config:cache后,考虑修改config/app.php,自定义服务提供者加载了boot()方法。

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题,我是这样解决的

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

        希望对你有用

        【讨论】:

          猜你喜欢
          • 2017-01-02
          • 1970-01-01
          • 1970-01-01
          • 2016-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-08
          相关资源
          最近更新 更多