Laravel 5.6 Auth /admin showing blank page with or

2019-08-05 18:53发布

问题:

I am new to Laravel and its 5.6 version. I setup a Auth login using few commands over there. All is good going. But when I uploaded to my own live server then when I run site.com/admin [that should be redirect to site.com/login for non login scene]. But I got a blank page. When I ran site.com.login then login page is working and I am in admin panel to work on. My question is that why my site.com/admin page is blank. all other pages are working fine.

here is my files.

routes/web.php

Route::get('/', 'HomeController@index')->name('home');;
Route::get('view/{slug?}', 'HomeController@view');

Auth::routes();
Route::prefix('admin')->group(function () {
    Route::get('/', 'Admin\AdminController@admin_index')->name('admin_index');
    Route::get('/dashboard', 'Admin\AdminController@dashboard')->name('dashboard');

    Route::get('locations', 'Admin\LocationsController@locations')->name('locations');
    Route::get('add-location', 'Admin\LocationsController@add_location')->name('add-location');
    Route::post('submit-location', 'Admin\LocationsController@submit_location')->name('submit-location');

    Route::get('posts', 'Admin\PostsController@posts')->name('posts');
    Route::get('add-post', 'Admin\PostsController@add_post')->name('add-post');
    Route::post('submit-post', 'Admin\PostsController@submit_post')->name('submit-post');
});
Route::get('/clear-cache', function() {
    $exitCode = Artisan::call('cache:clear');
});
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout')->name('logout');

app/Http/Kernel.php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ], 
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'is_admin' => \App\Http\Middleware\IsAdmin::class,
    ];
}

app/Http/isAdmin.php

namespace App\Http\Middleware;

use Closure;

class IsAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // return $next($request);
        if(auth()->user()->isAdmin()) {
            return $next($request);
        }
        return redirect('login');
        //return $next($request);
    }
}

AdminController.php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;  
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class AdminController extends Controller {

    public function __construct(){

        $this->middleware('auth');
    }

    public function admin_index() {

        return view('admin.index');
    }

    public function dashboard() {

        return view('admin.dashboard');
    }

    public function admin() {

        return view('admin.index');
    }
}

anything else I can share please let me know.

回答1:

Change:

Route::prefix('admin')->group(function () {
Route::get('/', 'Admin\AdminController@admin_index')->name('admin_index');
Route::get('/dashboard', 'Admin\AdminController@dashboard')->name('dashboard');

Route::get('locations', 'Admin\LocationsController@locations')->name('locations');
Route::get('add-location', 'Admin\LocationsController@add_location')->name('add-location');
Route::post('submit-location', 'Admin\LocationsController@submit_location')->name('submit-location');

Route::get('posts', 'Admin\PostsController@posts')->name('posts');
Route::get('add-post', 'Admin\PostsController@add_post')->name('add-post');
Route::post('submit-post', 'Admin\PostsController@submit_post')->name('submit-post');
});

To:

Route::group([
'middleware' => ['auth', 'is_admin'],
'namespace'  => 'Admin',
'prefix'     => 'admin',
], function () {
Route::get('/', 'AdminController@admin_index')->name('admin_index');
Route::get('/dashboard', 'AdminController@dashboard')->name('dashboard');

Route::get('locations', 'LocationsController@locations')->name('locations');
Route::get('add-location', 'LocationsController@add_location')->name('add-location');
Route::post('submit-location', 'LocationsController@submit_location')->name('submit-location');

Route::get('posts', 'PostsController@posts')->name('posts');
Route::get('add-post', 'PostsController@add_post')->name('add-post');
Route::post('submit-post', 'PostsController@submit_post')->name('submit-post');
});

And:

if(auth()->user()->isAdmin()) {

To:

if(optional(auth()->user())->isAdmin()) {

Be sure you have isAdmin() function in your User model which return true or false



回答2:

Your issue is that you are not using isAdmin middleware. You can use middleware on route to check the admin role.