1
votes

I'm setting up a laravel 5.3 application that uses custom packages and laravel authentication middleware. When I define routes in the laravel/packages/vendor/packageName/src/routes.php as is the case in

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

it redirects to localhost:8000/dashboard url defined in the RedirectIfAuthenticated Middleware but when I define the route in resources/routes/web.php, it routes and authorizes as required.

Is there anything i am doing wrong, or something I need to check?

---Update--- Below is a snippet from my ServiceProvider class

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/Config/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,

    ],

Output of php artisan route

enter image description here

2

2 Answers

1
votes

In Laravel 5.3, by using 'web' middleware group, session middleware is added to the route and auth works for me.

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

From the Laravel 5.3 docs:

To define routes for your package, simply require the routes file from within your package service provider's boot method. From within your routes file, you may use the Illuminate\Support\Facades\Route facade to register routes just as you would within a typical Laravel application:

Your problem is, that your routes.php file from your package is not included in your project. To achieve this, you should put the following code in your package's ServiceProvider:

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

Read more about it in the docs.

Update Try to make your route like the following (using group):

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