Laravel第一次重定向时出现404错误

时间:2019-02-12 00:25:26

标签: url-routing laravel-5.7 laravel-middleware laravel-authorization laravel-authentication

如果我去mysite.test/admin/,有时会出现404错误。我不明白为什么。

这是我的代码:

路线

/** 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');
    }
}

名为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('/');
    }
}

如果在出现404错误时刷新,则页面工作正常。我猜我在中间件上做错了什么,但我无法弄清楚是什么。


编辑

当我决定清除缓存时,该错误似乎消失了:

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

如果有人知道为什么会发生这种情况,请给出解释。 我将继续测试一段时间,如果该错误没有再次出现,请关闭此问题。

0 个答案:

没有答案