I wrote the code to fetch the JSON Data. For that I tried to do this in 2 projects.

  1. 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()->json(['CountryList' => \App\Models\CountryModel::all()]);
  1. 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" => $json_data["CountryList"]));

Then inside the View it is like below.

@foreach($Countries as $Country)
    <tr class="odd pointer">
        <td class=" ">{{$Country["Country"]}}</td>
        <td class=" ">{{$Country["CountryCode"]}}</td>

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

Question: Is there problem in below points?

  1. Code Quality
  2. Approach to fetch data in Presentation layer from API.
  3. 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

  • \$\begingroup\$ Could you please clarify what you are trying to achieve architecturally? Do you want a 3-tier/layer architecture? Or just some separation between the (data) model and presentation? \$\endgroup\$ Apr 4, 2016 at 5:29
  • \$\begingroup\$ I am looking for both 3 tier and code separation but to only some extent which will not cause performance issue. \$\endgroup\$
    – Pankaj
    Apr 4, 2016 at 5:30
  • \$\begingroup\$ You can put your database related functions mostly the queries to fetch or update the data from/in database in models. Like for the all() you can add one function in country model as getAllCountries() which will return all countries. Now you can easily used this model in both ways as you have mentioned. \$\endgroup\$ Apr 4, 2016 at 6:17

1 Answer 1


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.

enter image description here

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:


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.