Laravel带认证路由配置及首页空白异常问题求助
Hey Paul, let's work through your Laravel project issues together—we'll fix the blank page, unexpected modal inclusion, and get your authentication redirect logic sorted out.
First, let's tackle the root causes of your blank page and the random modal from create.blade.php:
Fix 1: Correct the Login Form's Action & Add CSRF Protection
Your login form is pointing to ../index.php, which isn't how Laravel handles requests. This can cause request failures and blank pages. Also, you're missing the required CSRF token, which Laravel uses to protect against cross-site attacks. Update your login form code:
<form action="{{ route('login') }}" method="post"> @csrf <!-- Add this mandatory CSRF token --> <div class="form-group"> <div class="col-xs-3 col-lg-6"> <label for="email">Email:</label> <input type="email" class="form-control" name="email" id="email"> </div> </div> <div class="form-group"> <div class="col-xs-3 col-lg-6"> <label for="password">Password:</label> <input type="password" class="form-control" name="password" id="password"> </div> </div> <p> <input type="submit" name="submit" value="Login" class="btn btn-primary btn-lg" role="button"> <!-- Fix the Register button (it was a reset input, which doesn't navigate) --> <a href="{{ route('register') }}" class="btn btn-success btn-lg" role="button">Register</a> </p> </form>
Fix 2: Stop Unconditional CKEditor Initialization
Your layout page runs CKEDITOR.replace('article-ckeditor') on every page, even when the element doesn't exist. This throws errors in the console that can block page rendering. Wrap the CKEditor script in a conditional check:
<!-- In your layout.app file --> <script src="{{asset('vendor/jquery/jquery.js')}}"></script> <script src="{{asset('vendor/bootstrap/js/bootstrap.min.js')}}"></script> <script src="{{asset('js/bootstrap-notify.min.js')}}"></script> <script src="{{asset('vendor/unisharp/laravel-ckeditor/ckeditor.js')}}"></script> @if(isset($enableCkeditor) && $enableCkeditor) <script> CKEDITOR.replace('article-ckeditor'); </script> @endif
Then, in views that actually need CKEditor (like posts/create.blade.php), add this at the top to enable it:
<?php $enableCkeditor = true; ?>
Fix 3: Track Down the Unexpected Modal
The random modal from create.blade.php is likely coming from one of these places:
- Check your
PagesController@indexmethod: Make sure it returns the correct home view (e.g.,return view('pages.index')) instead of accidentally loading the posts create view. - Inspect your
inc.navbarpartial: Ensure you didn't accidentally add@include('posts.create')or any modal code from that file here. - Clear view cache: Run
php artisan view:clearto wipe Laravel's cached views—sometimes old cached content causes weird rendering issues.
To make localhost redirect authenticated users to their dashboard and unauthenticated users to the login page (with a return to the intended page after login), use one of these approaches:
Option 1: Update the PagesController
Modify app/Http/Controllers/PagesController.php:
use Illuminate\Support\Facades\Auth; public function index() { if (Auth::check()) { // Redirect authenticated users to the dashboard return redirect()->route('dashboard'); } else { // Send unauthenticated users to login, preserving the intended return URL return redirect()->guest(route('login')); } }
Option 2: Use Route Middleware
Alternatively, handle this directly in your routes file:
use Illuminate\Support\Facades\Auth; Route::get('/', function () { if (Auth::check()) { return redirect()->route('dashboard'); } // Redirect to login with intended URL preserved return redirect()->guest(route('login')); }); Route::resource("posts", "PostsController"); Auth::routes(); Route::get('/dashboard', 'DashboardController@index')->name('dashboard');
Laravel's default Auth::routes() already handles returning users to the intended page after login, so this will work out of the box once you fix the login form.
- Run
php artisan route:listto confirm all routes are registered correctly. - Verify your
.envfile hasAPP_URL=http://localhostset. - Check your browser's developer console (F12) for any remaining JS/CSS errors—these are common culprits for blank pages.
内容的提问来源于stack exchange,提问作者Paul

