Search code examples
laravelapivalet

Communication between a Laravel front website a Laravel API, both under Valet


I have two locale installations of Laravel, one used as a front website and the other as an API, both are parked with Valet. My problem is that it doesn't seem to be able to communicate together, maybe because of Valet. So, I tried the little test below which, for some reason, is not working.

  • Step 1

I have this code in routes/api.php of the API website:

Route::get('hello-api', function () {
    return 'Hello Api!';
});

When opening the URL http://api.test/hello-api in my browser, I get "Hello Api!" as expected.

  • Step 2

Then, when I have the code below in the routes/web.php of the front website:

Route::get('/hello-front', function () {
     return 'foo';
});

When opening the URL http://front.test/hello-front in my browser, I get "foo" as expected.

  • Step 3

However, if I replace the code in routes/web.php of the front website by this one:

use GuzzleHttp\Client;

Route::get('/hello-api', function () {
//     return 'hello';

    $client = new Client();

    $response = $client->request('GET', 'http://api.test/hello-api');
    $statusCode = $response->getStatusCode();
    $body = $response->getBody()->getContents();

    return $statusCode;
    return $body;
});

Then, instead of the expected "Hello Api!", I get the following error message:

{"status":"error","errors":["cURL error 6: Could not resolve: api.test (Domain name not found) (see https:\/\/curl.haxx.se\/libcurl\/c\/libcurl-errors.html)"]}

What's wrong in my code or maybe in Valet ?


Solution

  • I finally fixed it by adding this to /etc/hosts:

    127.0.0.1       api.test
    

    But I'd be interested to see if it can be fixed in Valet.