Laravel 5.2根路径POST路由未列出及多表单提交问题
The core issue here is that Laravel can't distinguish between multiple POST routes defined for the exact same URI (/). When you register multiple routes with the same HTTP method and path, only the first one in your routes file will ever be triggered—any subsequent routes for that combination get ignored entirely.
Here are two straightforward solutions to resolve this:
Solution 1: Use Distinct URIs for Each Form
The simplest fix is to give each form its own unique endpoint. This keeps your routing clean and avoids any ambiguity.
Update your routes.php file to use separate paths:
Route::auth(); // Mails Route Route::get('/contact', ['uses' => 'PageController@getContact', 'as' => 'contact']); Route::post('/contact', ['uses' => 'PageController@postContact', 'as' => 'pages.postContact']); // Guest registration form Route::post('/guest-registration', ['uses' => 'PageController@postGuestRegistration', 'as' => 'pages.postGuestRegistration']); // Second form (replace with your actual method/name) Route::post('/second-form-action', ['uses' => 'PageController@postSecondForm', 'as' => 'pages.postSecondForm']);
Then update your Blade forms to point to these new routes using their named routes:
<!-- Guest registration form --> <form method="POST" action="{{ route('pages.postGuestRegistration') }}"> <!-- Form fields here --> </form> <!-- Second form --> <form method="POST" action="{{ route('pages.postSecondForm') }}"> <!-- Form fields here --> </form>
Solution 2: Use a Single Route with a Hidden Form Identifier
If you absolutely need both forms to submit to /, you can add a hidden input to each form to tell your controller which form was submitted.
First, add a hidden field to each Blade form:
<!-- Guest registration form --> <form method="POST" action="{{ route('pages.handleRootForms') }}"> <input type="hidden" name="form_type" value="guest_registration"> <!-- Other form fields --> </form> <!-- Second form --> <form method="POST" action="{{ route('pages.handleRootForms') }}"> <input type="hidden" name="form_type" value="second_form"> <!-- Other form fields --> </form>
Then define a single POST route in routes.php:
Route::auth(); // Mails Route Route::get('/contact', ['uses' => 'PageController@getContact', 'as' => 'contact']); Route::post('/contact', ['uses' => 'PageController@postContact', 'as' => 'pages.postContact']); // Single route to handle both root forms Route::post('/', ['uses' => 'PageController@handleRootForms', 'as' => 'pages.handleRootForms']);
Finally, add a controller method to route the request to the correct handler:
public function handleRootForms(Request $request) { switch($request->input('form_type')) { case 'guest_registration': return $this->postGuestRegistration($request); case 'second_form': return $this->postSecondForm($request); default: // Handle invalid form submission return redirect()->back()->with('error', 'Invalid form submission'); } } // Keep your existing handler methods public function postGuestRegistration(Request $request) { // Your guest registration logic here } public function postSecondForm(Request $request) { // Your second form's logic here }
Either approach will resolve the issue of your root POST routes not being recognized properly. The first method is generally preferred for clarity, but the second works if you have specific reasons to keep the same endpoint.
内容的提问来源于stack exchange,提问作者Eazy Sam

