1
votes

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

For example:

Route::group(['middleware' => ['web','auth']], function () {


Route::get('/post-aaron', 'ProductsController@postaaron');
Route::post('/post-aaron', 'ProductsController@savepostaaron');
Route::get('/aarons-posted', 'ProductsController@aaronsposted');
Route::get('/aaron-board', 'ProductsController@aaronboard');
Route::get('/edit-aaron/{id}', 'ProductsController@editaaron');
Route::get('/edit-aaron', 'ProductsController@updatepostaaron');

});

I want to make RESTFUL API for scaling across different platforms. How do I convert my existing web.php into api.php?

1
Your web routes return HTML right? And you want a 1-answer solution to turn this aggrevated data to a JSON api?online Thomas

1 Answers

1
votes

This depends on the code and logic in your controllers. Most likely, your controller are returing views. For example, return view('aaron-board'). However, a RESTful api will require a JSON response, something like return response()->json(['data' => 'users']).

It's not an easy task to convert a traditional web app to an api if you did not plan that from the beginning. However, it's doable.

Depending on the scale of your application, you may want to have dedicated controllers to process api requests or, if your application is small, you can have them processed by the current/same controllers. Another issue is routing. The middleware web will cause issues with api requests.

Here is what I would do as a starting point. I will adapt my controllers to response to api requests as well as normal request for the web application.

For example, let's say we have the following route:

// web.php

Route::get('/users', 'UsersController@index');

And our index method:

public function index(Request $request)
{
    $users = App\User::paginate();

    return view('users.index', compact('users');
}

We can basically change the index method to return a JSON response if the coming request is an api request as the follwing:

public function index(Request $request)
{
    $users = App\User::paginate();

    if ($request->wantsJson()) {

         return response()->json(['data' => $users]);     

    }

    return view('users.index', compact('users');
}

Now for the route, we'll duplicate the same route in the api.php file.

// api.php

Route::get('/users', 'UsersController@index');

This will give our application two routes:

  • www.example.com/users (will return a view)
  • www.example.com/api/users (will return a JSON response)

Note I'is important to set proper headers for the api requests to get the JSON response. i.e Accept: application/json. And also be mindful of CORS configurations.

As a side note, you're adding the web middleware to your routes in web.php file. This is fine but unnecessary. The middleware is already added by the RouteServiceProvider check the method mapWebRoutes at this file. You will see also that mapApiRoutes() adds the api middleware to all routes defined in the api.php file.