Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android App interaction with Laravel Action methods

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.

like image 887
Pankaj Avatar asked Mar 16 '16 08:03

Pankaj


People also ask

Can I create Android app using Laravel?

One of the best ways to develop a robust backend for your android app is to use the Laravel API on a cloud that does most of the backend processing. Laravel comes with a solid codebase and provides optimized performance for all lightweight and enterprise level applications.

Can I use Laravel for mobile app?

Firstly, you'll need to build an API from your Laravel app to use it as a 'back end'. You won't be able to use your existing codebase to create the front end of your mobile apps, so you will have to rebuild the features and functionalities of your existing web app for the iOS and Android platforms.

How to interact with other apps in Android?

To take the user from one activity to another, your app must use an Intent to define your app's "intent" to do something. When you pass an Intent to the system with a method such as startActivity() , the system uses the Intent to identify and start the appropriate app component.


1 Answers

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/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

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 view
public 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 JSON
public function getUsers($request)
{    
    $service = App::make( UserDataBuilder::class );
    $service->setUser( User::find(1)->get() );

    return response()->json( $data );
}
like image 109
Moppo Avatar answered Sep 21 '22 17:09

Moppo