Laravel 5.6中php artisan serve提交表单或加载页面缓慢求助
Hey there, let's walk through how to figure out why your form submission is loading so slowly when using php artisan serve for your Laravel 5.6 project. The fact that your command line logs only show static asset requests (CSS/JS) but no sign of the form's POST request is a great starting clue—let's break this down step by step:
Verify the form is actually sending the request
First, double-check your HTML form code. Make sure theactionattribute points to the correct Laravel route (watch for typos!), and themethodis set toPOST. Use your browser's DevTools (Network tab) to monitor the submission—if the request doesn't show up here, the issue might be frontend-related. Check the Console tab for JavaScript errors, especially since you're using Materialize (sometimes uninitialized components can block form submissions).Check if the request reaches your Laravel backend
If the browser shows the request is pending but yourphp artisan servelogs don't list the POST request, there's likely a routing problem. Openroutes/web.phpand confirm your POST route exists and matches the form's action. Also, check if any middleware attached to the route is causing delays—like a custom middleware that's waiting on an unresponsive external service.Pinpoint slow code in your controller
If the request does reach your backend, add debug logs to your form-handling controller method to see where it gets stuck. For example:use Illuminate\Support\Facades\Log; public function handleFormSubmission(Request $request) { Log::info('Form submission started', ['request_data' => $request->all()]); // ... rest of your processing code Log::info('Form submission finished'); }Check
storage/logs/laravel.log—if only the first log appears, the problem lies in the code between those two lines. Common culprits here are slow external API calls, inefficient database queries (like N+1 problems), or accidental infinite loops.Adjust PHP execution limits
The PHP built-in server (used byphp artisan serve) has default execution time limits. If your form processing exceedsmax_execution_timein your php.ini, it might hang. You can temporarily override this in your controller withset_time_limit(300);(sets a 5-minute limit) to test, or adjust the value directly in your php.ini file.Fix session/cache permissions and clear stale data
Laravel stores sessions instorage/framework/sessionsby default. If this directory has incorrect permissions, session writes can slow down or fail. Runchmod -R 755 storage/to fix permissions. Also, clear cached data with these commands:php artisan cache:clear php artisan config:clear php artisan route:clearTroubleshoot database connection issues
If your form interacts with the database, a slow or misconfigured connection could be the issue. Check your.envfile: setDB_HOSTto127.0.0.1instead oflocalhost(sometimes localhost uses a slower socket connection instead of TCP/IP). You can also time your database queries to spot bottlenecks:$start = microtime(true); // Your database query here $end = microtime(true); Log::info('Query execution time: ' . ($end - $start) . ' seconds');
内容的提问来源于stack exchange,提问作者Adib Aulia Rahmansyah

