Fresh install of Laravel 5.1 using laravel installer. I require dingo API through composer using
composer require dingo/api:0.10.*@dev. (commit edf4b50)
After that I configure in my .env:
API_DOMAIN=local.l5.api.server.com
#API_DOMAIN=local.l5.api.server.com:44300
API_VERSION=v1
API_CONDITIONAL_REQUEST=false
API_STRICT=false
API_DEBUG=true
My routes.php
/**
* @var \Dingo\Api\Routing\Router $api
*/
$api = app('api.router');
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
*/
$params = [
'version' => 'v1',
//'prefix' => 'api',
'namespace' => 'App\\Http\\Controllers',
];
$api->group($params, function($api)
{
$api->get('users', 'UsersController@index');
});
If I point my browser to https://local.l5.api.server.com:44300/users I get:
{
"message": "404 Not Found",
"status_code": 404,
"debug":
{
"line": 143,
"file": "/home/vagrant/Code/l5.api.server.com/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
"class": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
"trace": [...]
}
}
Now, if I comment out on my .env the API_DOMAIN config and add the API_PREFIX and edit my route to add that prefix then it works fine and I get a response back. So I'm having this issue only when having a domain configured. I've tried putting my domain with and without port number (Homestead works with ports 8000 and 44300) but have no change at all.
I really don't know what else to try. I have done 3 fresh installs already following step by step the wiki and nothing changes. With API_PREFIX it works fine, without prefix but with API_DOMAIN it does not. Maybe I'm missing something.
Thanks in advance for any help anyone can give.
You'll need to add the domain, much like how you add the prefix, to your API group.
$params = [
'version' => 'v1',
'domain' => 'local.l5.api.server.com'
'namespace' => 'App\\Http\\Controllers',
];
Actually you shouldn't _need_ to specify it. I'll take a closer look at this.
Well, domains are working as expected from all my testing. Granted, I'm not using any ports. Sounds like the initial request middleware is failing though.
Can you open up Dingo\Api\Http\Validation\Domain and in the validate method, add this before the return:
dd($this->domain, $request->header('host'));
Just check to see what values are being reported here.
Thanks for your answer!
First test is as you said, if I add the domain as part of the API group _without_ port number; and set API_DOMAIN to the domain _with_ port number then it works.
// API_DOMAIN=local.l5.api.server.com:44300
$params = [
'version' => 'v1',
'domain' => 'local.l5.api.server.com',
'namespace' => 'App\\Http\\Controllers',
];
As to actually needing it or not, here's the dump of what you asked:
// route
$api->group(['version' => 'v1', 'namespace' => 'App\\Http\\Controllers'], function($api)
{
$api->get('users', 'UsersController@index');
});
// dd($this->domain, $request->header('host'));
// API_DOMAIN=local.l5.api.server.com:44300
"local.l5.api.server.com:44300"
"local.l5.api.server.com:44300"
// API_DOMAIN=local.l5.api.server.com
"local.l5.api.server.com"
"local.l5.api.server.com:44300"
Cheers mate!
Yeah, looks like you just need to set the port. So, adding it with the port to .env, and removing the domain from your $params array doesn't work?
Exactly, it doesn't work.
It ended up working with the port set in .env and domain with no port in
$params.
On Aug 11, 2015 00:12, "Jason Lewis" [email protected] wrote:
Yeah, looks like you just need to set the port. So, adding it with the
port to .env, and removing the domain from your $params array doesn't
work?—
Reply to this email directly or view it on GitHub
https://github.com/dingo/api/issues/570#issuecomment-129688188.
That's weird. I'll see if I can do anything but might be something weird with ports.
Great, thanks!
I'll see if I can dig deeper during the weekend. If I found anything I'll try to make a PR.
Hi @jasonlewis, same issue here.
I'm using Laravel Homestead for development, so I use port 8000.
Thanks!
@josemf I've got it working with the following config (as per @jasonlewis input):
Config in .env is as follows:
APP_DOMAIN=local.api.server.com
API_DOMAIN=local.api.server.com:44300 #notice that only API_DOMAIN has the port number
Your API routes should be configured as follows:
/** @var \Dingo\Api\Routing\Router $api */
$api = app('api.router');
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
*/
$params = [
'as' => 'api::',
'version' => 'v1',
'domain' => env('APP_DOMAIN'), // Notice we use the domain WITHOUT port number
'namespace' => 'App\\Http\\Controllers',
];
$api->group($params, function($api)
{
// $api routes here
}
When testing (I run my tests within the VM) both API_DOMAIN and APP_DOMAIN should not contain the port number. This could be configured in phpunit.xml:
<env name="API_DOMAIN" value="api.server.com"/>
<env name="APP_DOMAIN" value="api.server.com"/>
Hope this helps!
Thank you @eduacostam, got it working now!
I experienced the same thing, thanks for posting a solution. Dingo seems really useful but it's discouraging to have these problems early on. :(
If you're using Homestead @josemf are you adding your sites to your /etc/hosts file. You don't _need_ to use port 8000.
That said I still want to find out why it's requiring a weird config like this.
Okay so I've been testing with ports, both 8000 and 44300 and it seems to have no issues. So long as you define the API_DOMAIN with the port number it will be fine. No need to set a domain key on the group options.
My routes:
$api->version('v1', function($api) {
$api->get('users', function () {
return ['foo' => 'bar'];
});
});
My .env file:
API_DOMAIN=local.l5.api.server.com:44300
Note that that's for HTTPS, changing it to 8000 works exactly the same. I had no problems. Didn't need to define anything on the route group.
Visiting https://local.l5.api.server.com:44300/users returned the JSON response.
@jasonlewis I actually need the domain key, don't know why, since you've reproduced my settings..
Do you want me to try anything here, so you'll be able to test further?
I've got no idea mate. Only thing I can recommend is a fresh install even though you've mentioned in the OP that that's what you started with. So, beats me.
I'm not sure what else we can do here so going to close for now.
I realize this topic has been closed for some time now but I wanted to share my experience as well.
I also develop in a VirtualBox/Vagrant environment in a Homestead box. I do something a bit different though...I actually map every site to it's own unique port so that I can expose a single site to the public using Vagrant's share feature.
So like @josemf and @eduacostam have stated, I am only able to get this working when I specify my API_DOMAIN in .env as api.mysite.com:8022 and then in routes.php I have to also specify the domain in the params WITHOUT the port:
$api->group(['domain' => 'api.mysite.com'], function ($api) {
$api->get('users', function () {
return ['foo' => 'bar'];
});
});
I have tried every variation and possibility I could think of to get this working without specifying the domain in the params but it just doesn't work.
@jeitnier Holy CRAP...
THANK YOU.
I've been at this for the last I-don't-know-how-many hours, but thanks to your post, I've finally gotten the damn thing working.
This issue really needs attention.
@shiruken1 I'm so glad I could help someone. I was pulling my hair out for hours trying every different combination of settings to get it working!
@jeitnier :+1: You are a gentleman and a scholar!
@jeitnier how do you configure the .env to run both locally and in production? i'm having a hard time figuring out how to make that work. i redirected api.mysite.com via the hosts file and i can get it to work using your method locally, but not in production.
i'd like my api to be hosted at api.mysite.com/<endpoints>. any guidance you can offer would be greatly appreciated.
@jasonlewis , unable to find Dingo\Api\Http\Validation\Domain
THANK YOU. Really Awesome!
@jasonlewis , unable to find
Dingo\Api\Http\Validation\Domain
dingo\apisrc\Http\Validation
@jeitnier old news I know, but I've recently been banging my head while using docker on a non standard port.
The call to specify the domain in the params WITHOUT the port, huge. Thanks!
Most helpful comment
I realize this topic has been closed for some time now but I wanted to share my experience as well.
I also develop in a VirtualBox/Vagrant environment in a Homestead box. I do something a bit different though...I actually map every site to it's own unique port so that I can expose a single site to the public using Vagrant's
sharefeature.So like @josemf and @eduacostam have stated, I am only able to get this working when I specify my
API_DOMAINin.envasapi.mysite.com:8022and then inroutes.phpI have to also specify thedomainin the params WITHOUT the port:I have tried every variation and possibility I could think of to get this working without specifying the
domainin the params but it just doesn't work.