api: "404 Not Found" when using API_DOMAIN. With API_PREFIX works fine.

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.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 1
  • Comments: 25 (12 by maintainers)

Most upvoted comments

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.

@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!

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.