I am building a RESTful API for the first time using Laravel 5.8.
I want my routes/api.php to point to api.mysite.test
My question is very similar to Laravel 5.8 use subdomain as API endpoint beside domain.com/api
The accepted answer to the question above is
As for the API subdomain, I would just park it on top of the main domain. Then you don't have to worry about what directory it's pointed at.
I don't understand it very well, but I want to be able to use mysite.test as a web interface, presenting the API.
So far I removed the prefix from mapApiRoutes in RouteServiceProvider.php and tried to concatenate the subdomain
protected function mapApiRoutes()
{
Route::domain('api.'.url('/'))
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
My routes/api.php:
<?php
use Illuminate\Http\Request;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('resources', 'ResourceController');
In /etc/hosts:
192.168.10.10 mysite.test
In .env:
APP_URL=mysite.test
In config/app.php
'url' => env('APP_URL'),
When I try to reach my resource
GET api.mysite.test/resources results in Could do not get any response
Also bothers me that
>>> url('/')
=> "http://localhost/mysite.test"
instead of mysite.test as I stated in .env
192.168.10.10 api.mysite.testto/etc/hostsfor GET api.mysite.test/resources I get HTML response with status '404 not found'Route::domain('api.mysite.test')in mapApiRoutes works fine, but whyurl('/')generates"http://localhost/mysite.test"? It's wierd?