Android App interaction with Laravel Action methods Android App interaction with Laravel Action methods laravel laravel

Android App interaction with Laravel Action methods


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

Routes

First of all you need to 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/userspublic 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/userspublic 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

About the Service class

Regarding the service class i've mentioned, it could be simply one or multiple wrapper classes that you use both in API and APP controllers to build and get the data without repeting code. The structure of such classes depends on how your app work.

For example let's suppose you need to compute some data for each user's project, store it in a variable and then send it to the viev:

public function getUsers($request){    $user = Users::with('projects')->find( $request->user_id )->get();    $data = [];    foreach ( $user->projects as $p )    {         //compute some data you need and store it in $data;    }    return View("View-Path", array("Data" => $data)); }

Now if want to make the same thing in the API controller, you'd need to repete this code to get the projects and create the data. To avoid this, you could 'wrap' the data access in a service class, and use the same class in boh controllers:

Service class

public class UserDataBuilder{    protected $user;    public function setUser( Authenticatable $user )    {        $this->user = $user;    }    public function getData()    {        $user = Users::with('projects')->find( $this-user->id )->get();        $data = [];        foreach ( $user->projects as $p )        {             //compute some data you need and store it in $data;        }        return $data;    }}

and use the same class in both API and APP controllers:

//APP controller: get the data and pass to the viewpublic function getUsers($request){            $service = App::make( UserDataBuilder::class );    $service->setUser( User::find( $request->user_id )->get() );    return View("View-Path", array("Data" => $service->getData() ); }//API controller: get the data and convert to JSONpublic function getUsers($request){        $service = App::make( UserDataBuilder::class );    $service->setUser( User::find(1)->get() );    return response()->json( $data );}


For android you have to write a separate web service.


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));}