seankonig
seankonig

Reputation: 53

Why would the order of Laravel API routes affect how they work

I have the following routes setup with an auth prefix.

// Workspace Routes
        Route::get('workspace/region-setup-status', 'WorkspaceController@regionSetupStatus');
        Route::get('workspace/regions', 'WorkspaceController@getRegions');
        Route::get('workspace/{id}', 'WorkspaceController@show');
        Route::post('workspace/store-regions', 'WorkspaceController@storeRegions');

        Route::get('workspace/operations', 'WorkspaceController@getOperations');
        Route::get('workspace/operation-setup-status', 'WorkspaceController@operationSetupStatus');
        Route::post('workspace/store-operations', 'WorkspaceController@storeOperations');

        Route::get('workspace/inspection-area-setup-status', 'WorkspaceController@inspectionAreaSetupStatus');
        Route::post('workspace/store-inspection-areas', 'WorkspaceController@storeInspectionAreas');

In this setup the get route for 'workspace/operation-setup-status' refuses to return anything from the method.

However if I reorder the routes and move the 'operation' routes to above the 'region' routes everything works as expected?

Why would it behave this way?

Upvotes: 0

Views: 42

Answers (1)

Alberto
Alberto

Reputation: 12939

Because Laravel try to do a pattern matching with the routes, with the order which the routes are declared, and the first that make a match, it runs that one, so your workspace/operation-setup-status is managed by

Route::get('workspace/{id}', 'WorkspaceController@show'); // $id === "operation-setup-status"

but if you move your

Route::get('workspace/operation-setup-status', 'WorkspaceController@operationSetupStatus');

before the one with {id} , it match correctly in the right order

Upvotes: 4

Related Questions