0

If I go to mysite.test/admin/ sometimes it gives me a 404 error. I cannot understand why.

Here is my code:

Routes

/** Admin Routes */
Route::group(array('prefix' => 'admin'), function()
{
    Route::get('/', 'Admin\PagesController@home');
});
/** User Routes */
// Pages
Route::get('/', 'PagesController@home');
Route::get('/test', 'PagesController@test');

Controllers\Admin\PagesController

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PagesController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('admin');
    }
    public function home()
    {
        dd('admin page');
    }

    public function test()
    {
        return view('mobile/test');
    }
}

Controllers\PagesController

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PagesController extends Controller
{
    public function home()
    {
        $venues = Venue::all();

        return view('mobile/index', [
            'venues' => $venues,
            'page_title' => 'Home'
        ]);
    }

    public function test()
    {
        return view('mobile/test');
    }
}

Middleware called Admin

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class Admin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ( Auth::check() && Auth::user()->isOwner == 1) {
            return $next($request);
        }

        \Session::flash('message', 'You are not an Admin');
        \Session::flash('alert-class', 'static-notification-red');
        return redirect('/');
    }
}

If I refresh when I get the 404 errors, then the page works fine. I guess I have done something wrong with the middleware but I cannot figure out what.


EDIT

It seems that the bug disappeared when I decided to clear the cache:

php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan config:clear

If anyone understands why this happened, please give an explanation. I will keep testing it for some time and close this question if the bug does not reappear.

padawanTony
  • 1,348
  • 2
  • 22
  • 41
  • Try removing the Auth middleware in admin controller as you're checking for login in your admin middleware – Sapnesh Naik Feb 12 '19 at 05:47
  • @SapneshNaik thanks for the suggestion. I will try it soon. But, here you suggest my approach: https://stackoverflow.com/questions/48660790/multiple-auth-for-laravel/48660927#48660927 – padawanTony Feb 12 '19 at 14:59
  • @SapneshNaik so I tried it what you suggested and it didn't work. Once I get the required functionality and once I get 404 – padawanTony Feb 12 '19 at 15:42

0 Answers0