Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

namaan's avatar

Package controller not called when using middleware('auth'), while signed in.

Hello,

I'm still a bit new at this, and thought the best way to get my feet wet would be to try and create a package. Everything's good so far, and a view is being served at the /moddler route, so long as I don't add ->middleware('auth') to either the route nor the controller constructor. And this is while I'm signed in. So if I go to /moddler while signed in, it just redirects to /home instead. I'm probably missing something basic here, but would appreciate it if someone could point me in the right direction.

This works:

Route::get('/moddler', 'Moddler\ModdlerController@index');
<?php
namespace Moddler;

use App\Http\Controllers\Controller;

class ModdlerController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('moddler::test');
    }
}

But neither of these work:

Route::get('/moddler', 'Moddler\ModdlerController@index')->middleware('auth');
<?php
namespace Moddler;

use App\Http\Controllers\Controller;

class ModdlerController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('moddler::test');
    }
}

Many thanks!

0 likes
2 replies
SlimDeluxe's avatar

This is an old question but today with 6.9 I am having the exact same issue. If I put the route in the global routes/web.php it works, however when I put it in the package's routes file, it fails to detect that I am logged in and redirects to /login, but then it sees I am logged in so it redirects to /home. It does not matter if you use the middleware on the route or in the controller constructor. How did you solve this?

SlimDeluxe's avatar

The solution is here - you have to include the web middleware group, since otherwise the session is not started I guess.

Please or to participate in this conversation.