Friday, March 25, 2016

Android App interaction with Laravel Action methods

Leave a Comment

I already have Laravel web pages where i can add/update/delete/Read records from MySQL Database. Laravel version is 5.2.15

Now, I have to integrate Database with Android App. In order to do that I have to post and read Json Data.

Here question is: Should I have 2 public action methods? First for web page that will show records on webpage and second will return json data in Android.

I meant, when I return data to webPage..I will have to write the below code.

return View("View-Path", array("Data" => $Data)); 

but in case of Android App, I will have to request Json Data.

Please suggest the right approach.

3 Answers

Answers 1

For android you have to write a separate web service.

Answers 2

Probably you should develop a simple API to access your APP data from an android client:

Routes

First of all you should create some specific routes for the API through which you'll serve your data in JSON format

Authentication

The API's routes should handle authentication in a different way in respect on what you're doing now: you can't use the classic session-based approach. Instead you have to use a basic or token-based approach. You've different alternatives, these are some of the most used (from the simplest, to the most complicated )

Laravel HTTP Basic Authentication

Laravel Json Web Token Authentication

Laravel OAUTH2

Data Acess

Once you've setup your routes and authentication, you have to serve your data via the API routes. Since you use the same data in your APP routes and API routes, you can wrap the logic of data building and retrieving in services, and use the services to get the data both in your APP routes and API routes.

Using different controllers for API and APP routes, you have:

//APP Controller method for route: www.app.com/app-route/users public function getUsers() {     //wrap the logic to build the data inside the service     $service = App::make('your_service');      //once is ready, get the built data from the service     $data = $service->getData();       return View("View-Path", array("Data" => $data));  }  //API Controller method for route: www.app.com/api/users public function getUsers() {     //use the same service to build and get the data you need     $service = App::make('your_service');      $data = $service->getData();       return response()->json( $data ); } 

This way you can:

  • Encapsulate data building and retrieveng in services, so that you don't have the need to duplicate code for data retrieving between APP and API routes

  • Have different controllers to access APP or API routes; so you can get the data, transform it as you need and serve it to either views or api clients

Answers 3

You can use one method to do that. Example

public function someMethod(Request $request){      $Data = ['laravel','is','awesome'];      // If the request has the content type is json return a json response     // So you android request will be true for this request      // else return data to webpage     if($request->isJson()){         return response()->json($Data);     }      return View("View-Path", array("Data" => $Data));  } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment