【问题标题】:Laravel middleware redirects all auth routes in packages to dashboardLaravel 中间件将包中的所有身份验证路由重定向到仪表板
【发布时间】:2017-02-25 20:28:32
【问题描述】:

我正在设置一个使用自定义包和 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 类中的一个 sn-p

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 的输出

【问题讨论】:

    标签: laravel laravel-routing laravel-5.3 laravel-middleware laravel-authorization


    【解决方案1】:

    在 Laravel 5.3 中,通过使用 'web' 中间件组,会话中间件被添加到路由中,并且身份验证对我有用。

    Route::group(['middleware' => ['web','admin']], function () {
         Route::get('/admin/somepage', '\MyPackage\SomeController@somepage');
    });
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 感谢 Jan 的回复。我真的很感激。
      • 您好 Jan,我刚刚更新了问题以显示 routes.php 文件已按要求发布。路由从包中运行良好,当我引入 auth 中间件以强制对包路由进行身份验证时,就会出现问题。
      • 你能从你的 app/config/app.php 发布你的“providers”配置吗?
      • 嗨 Jan,我已经添加了“providers”配置。感谢您的持续关注。我非常感谢。
      • 嗨 Jan,我已经尝试过您指定的组方法,它仍然会重定向到仪表板。
      猜你喜欢
      • 2019-06-30
      • 2018-07-25
      • 2019-02-09
      • 1970-01-01
      • 2020-07-06
      • 2021-06-28
      • 2021-05-25
      • 2022-01-24
      • 2021-08-09
      相关资源
      最近更新 更多