What's The Best Practice For Having A Laravel Application Serve Both The Browser-based Application, And Act As An API For The IPhone And Android App?

We use repository pattern in Laravel 5.1 and kept our logic in repository and we return the data from repo to controller, how can we use same data for Native app and web browser (i.e. web application) course data is in json so how can we show same data in blade we need to use another framework for front end (Angular or any other)?



I personally don't think you would need a package or framework to achieve this. Since you are following the repository pattern, your controllers are only in charge of returning results (optionally transforming them for output too).

To achieve your goal;

1- Group your API routes (etc. Route::group('api/v1', ...)

2- Create a class or even better a Trait to detect incoming routes

3- Either within your controller or on the class that handles JSON conversion, detect the route of the request.

4- If the request is being made from the api route, return JSON. Blade template otherwise.

You can also check out this link, which will help you building up your route detection. Also remember that you would need this exact implementation wihin your ExceptionHandler as well. You wouldn't want someone to see a json error message on your web front end, or a 404 blade template being returned from your API.