1

我正在设置一个使用自定义包和 laravel 身份验证中间件的 laravel 5.3 应用程序。当我在 laravel/packages/vendor/packageName/src/routes.php 中定义路由时

Route::get('member/list', function() {
    return 'member lists here';
})->middleware('auth');

它重定向到在 RedirectIfAuthenticated 中间件中定义的 localhost:8000/dashboard url,但是当我在 resources/routes/web.php 中定义路由时,它会根据需要进行路由和授权。

有什么我做错了吗,或者我需要检查什么?

---更新---下面是我的 ServiceProvider 类的一个片段

namespace Beansoft\PractitionerIMS;
use Illuminate\Support\ServiceProvider;
class PractitionerIMSServiceProvider extends ServiceProvider {
    public function register() {
        $this->app->bind('practitionerims', function($app) {
            return new PractitionerIMS;
        });
    }

    public function boot() {
        //load the routes file
        if (!$this->app->routesAreCached()) {
            require __DIR__ . '/Http/routes.php';
        }
}

应用程序/配置/app.php

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        Beansoft\PractitionerIMS\PractitionerIMSServiceProvider::class,


        /*
         * Package Service Providers...
         */

        //
        Yab\Laracogs\LaracogsProvider::class,

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

    ],

php artisan route 的输出

在此处输入图像描述

4

2 回答 2

1

在 Laravel 5.3 中,通过使用 'web' 中间件组,会话中间件被添加到路由和身份验证为我工作。

Route::group(['middleware' => ['web','admin']], function () {
     Route::get('/admin/somepage', '\MyPackage\SomeController@somepage');
});
于 2018-07-19T17:11:46.217 回答
0

来自 Laravel 5.3 文档:

要为您的包定义路由,只需从您的包服务提供商的引导方法中获取路由文件。在你的路由文件中,你可以使用 Illuminate\Support\Facades\Route 门面来注册路由,就像在典型的 Laravel 应用程序中一样:

您的问题是,您的包中的routes.php文件未包含在您的项目中。为此,您应该将以下代码放入包的ServiceProvider中:

public function boot()
{
  if (! $this->app->routesAreCached()) {
      // customize this reference according to your package structure
      require __DIR__.'/../../routes.php';
  }
}

在docs中阅读有关它的更多信息。

更新 尝试使您的路线如下所示(使用组):

Route::group(['middleware' => 'auth'], function() {
  Route::get('member/list', function() {
    return 'member lists here';
  });
});
于 2016-10-17T07:06:46.520 回答