首页 文章

Laravel中间件将包中的所有auth路由重定向到仪表板

提问于
浏览
0

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

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

它重定向到RedirectIfAuthenticated Middleware中定义的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路线的输出

enter image description here

2 回答

  • 0

    来自Laravel 5.3文档:

    要定义程序包的路由,只需要从程序包服务提供程序的引导方法中获取routes文件 . 在路径文件中,您可以使用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中阅读更多相关信息 .

    Update 尝试按照以下方式制作您的路线(使用群组):

    Route::group(['middleware' => 'auth'], function() {
      Route::get('member/list', function() {
        return 'member lists here';
      });
    });
    
  • 0

    在Laravel 5.3中,通过使用“web”中间件组,会话中间件被添加到路由中,auth为我工作 .

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

相关问题