lundi 1 octobre 2018

Laravel 5: API routes + wildcard route results in unexpected behavior

I am trying to build a React application backed with Laravel API, so essentially using a wildcard route for client-side routing, and then simply an API route group to process data.

This is my routes/web.php file:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/payment/redirect/{orderId}', ['as' => 'mollie.redirect', 'uses' => 'Controller@index']);
Route::get('/{any}', ['as' => 'index', 'uses' => 'Controller@index'])->where('any', '.*');

And this is my routes/api.php file:

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::post('/orders', ['as' => 'orders.store', 'uses' => 'OrdersController@store']);
Route::post('/payment/webhook', ['as' => 'mollie.webhook', 'uses' => 'OrdersController@webhook']);

which results in:

enter image description here

But, whenever I try to make a request at POST api/orders this is what I get in Postman:

enter image description here

Which is what Controller@index should respond, not OrdersController@store, which should be a JSON response.

Also, if I try to remove the API routes temporarily, and still attempt to access them, I weirdly enough get a 404, which means Laravel is able to detect the route, but it's using the wrong Controller's response.

How do I fix this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire