Accessing Data inside Presentation Layer from API: Laravel 5.2.27 I wrote the code to fetch the JSON Data. For that I tried to do this in 2 projects.


  
  First Laravel project which has API code for database interaction only. 
  


Sample code in API controller

class CountryAPI extends Controller
{
    public function AllCountries() {
        return response()-&gt;json(['CountryList' =&gt; \App\Models\CountryModel::all()]);
    }
}



  
  Second Laravel project which is a Presentation Layer that has code to access data from an API. So, Database is not exposed directly in this
  layer, instead a URL is being used to access data.
  


Sample code in the Presentation Layer 

class CountryController extends Controller {

    public function AllCountries() {
        $url = "http://localhost/API/public/Countries";
        $json = file_get_contents($url);
        $json_data = json_decode($json, true);
        return view('Country.List', array("Countries" =&gt; $json_data["CountryList"]));
    }
}


Then inside the View it is like below.

@foreach($Countries as $Country)
    &lt;tr class="odd pointer"&gt;
        &lt;td class=" "&gt;{{$Country["Country"]}}&lt;/td&gt;
        &lt;td class=" "&gt;{{$Country["CountryCode"]}}&lt;/td&gt;
    &lt;/tr&gt;
@endforeach


This is just a beginning to learn how to isolate the database code from Presentation Layer.

Question: Is there problem in below points?


  
  Code Quality 
  Approach to fetch data in Presentation layer from API. 
  As website users will send request to website and then website will send request to Database server through an API. So each requests will have one in direct additional request. Is that good?
  


Reason I asked this because Data has to be accessed from Android as well as from Website
This looks like a good place to use a shared repository. If you used a repository that was shared between your android api server app and your web server app you could avoid the need for inter-server communications and translating to and from json. You'd get better performance and the repository would function as the middle tier of your 3-tier architecture.



Repositories in Laravel are a fairly large topic so I'll just point you at Laracasts for that. For using a shared model in more than one project, this thread gives an example of doing it via composer: 

https://laracasts.com/discuss/channels/general-discussion/share-models-between-two-laravel-projects?page=1

That's not to say there is anything particularly wrong with the architecture you're describing. It shouldn't have too much of a performance hit but it will have a hit from the communications aspect and the translation to and from json aspect. Both of which are small as long as you stay within your local LAN. You seem to be accessing localhost for the API so it shouldn't be a problem.