Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

leviathanz's avatar

Is it possible to convert existing web.php to api.php (RESTFUL API)

I have a set of currently working routes on Laravel 5.6 (they are either called routes or web.php).

For example (web.php) route:

Route::group(['middleware' => ['auth', 'active']], function() {
	Route::post('sales/sale-data', 'SaleController@saleData');
	Route::post('api/sale-data', 'SaleController@saleData');
	Route::post('sales/sale-history', 'SaleController@saleDataHistory');
	Route::post('sales/sendmail', 'SaleController@sendMail')->name('sale.sendmail');
	Route::get('sales/sale_by_csv', 'SaleController@saleByCsv');
	Route::get('sales/product_sale/{id}','SaleController@productSaleData');
});

I want to make RESTFUL API for scaling across different platforms. How do I convert my existing web.php into api.php? Is it possible? I tried to put this script on existing controller:

if ($request->wantsJson()) {

         return response()->json(['saleData' => $sales]);     

    }

and also tried this on controller:

		if (Request::is('api*')) {
			return response()->json($request->saleData());
		}else{
			echo json_encode($json_data);
		}

but it return not found.

and in my api.php route:

	Route::group(['middleware' => ['auth', 'active']], function() {
		Route::resource('api/sales/sale-data', 'SaleController');	
    });
0 likes
1 reply
ajohnson6494's avatar
Level 11

I'm guessing that your issue is in the api.php file that you are starting your route with api.

Laravel by default prepends api/ to any route in the api.php route file. So your route is actually /api/api/sales/sale-data

Does that work?

Please or to participate in this conversation.