Api: Cannot link to laravel's routes

Created on 21 Mar 2016  路  23Comments  路  Source: dingo/api

I have a normal route like this:

Route::get('browse', [
    'as' => 'browse',
    'uses' => 'HomeController@browse',
]);

Normally I can generate url by using route('browse') or action('HomeController@browse') but now it doesn't work with API's routes.

Most helpful comment

@amirhabibzadeh API_DOMAIN should ONLY be used when you're using Laravel and have a virtual host configured for the sub-domain, for example, api.yourapp.com.

If you're using API_DOMAIN then API_PREFIX should be null.

If you're API is served from yourapp.com/api then you'll want to be using API_PREFIX.

All 23 comments

Dingo has it's own UrlGenerator to help generate links to routes/actions. It's bound as api.url in the container.

There's a version helper function to shorten the calls somewhat.

app('api.url')->version('v1')->route('api.route.name');

version('v1')->route('api.route.name'); // same as above

You need to define the version first so it can pull in the right route collection.

I want to generate link for laravel routes, not API routes. I can't use the helper if the end point was handle by this package.

After update i have this problem too , dingo api routes overwrites laravel routes. Here is an example;

//app/Providers/RouteServiceProvider.php
 public function map(Router $router)
    {
        $router->group(['namespace' => $this->namespace], function ($router) {
            require app_path('Http/apiroute.php');
            require app_path('Http/routes.php');
        });
    }
//app/Http/routes.php
Route::get('/', function () {
    return view('welcome');
});
//app/Http/apiroutes.php
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
    $api->group(['namespace' => 'Api\Controllers', 'middleware' => 'cors'], function ($api) {
        $api->post('login', 'AuthController@authenticate');
        $api->group( [ 'middleware' => 'jwt.refresh' ], function ($api) {
            $api->get('users/me', 'AuthController@me');
            $api->get('auth/refresh', 'AuthController@refresh');
            $api->get('validate_token', 'AuthController@validateToken');
            //$api->get('tickets', 'TicketController@index');
            $api->resource('tickets','TicketController');
            $api->resource('reply','ReplyController');
        });
    });
});

Here is what happens when i try /

{"message":"404 Not Found","status_code":404,"debug":{"line":161,"file":"\/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/RouteCollection.php","class":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException","trace":["#0 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php(823): Illuminate\\Routing\\RouteCollection->match(Object(Dingo\\Api\\Http\\Request))","#1 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php(691): Illuminate\\Routing\\Router->findRoute(Object(Dingo\\Api\\Http\\Request))","#2 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php(675): Illuminate\\Routing\\Router->dispatchToRoute(Object(Dingo\\Api\\Http\\Request))","#3 \/home\/vagrant\/Code\/Api\/vendor\/dingo\/api\/src\/Routing\/Adapter\/Laravel.php(56): Illuminate\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))","#4 \/home\/vagrant\/Code\/Api\/vendor\/dingo\/api\/src\/Routing\/Router.php(559): Dingo\\Api\\Routing\\Adapter\\Laravel->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')","#5 \/home\/vagrant\/Code\/Api\/vendor\/dingo\/api\/src\/Http\/Middleware\/Request.php(123): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))","#6 [internal function]: Dingo\\Api\\Http\\Middleware\\Request->Dingo\\Api\\Http\\Middleware\\{closure}(Object(Dingo\\Api\\Http\\Request))","#7 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(139): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))","#8 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Middleware\/CheckForMaintenanceMode.php(44): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))","#9 [internal function]: Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))","#10 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(124): call_user_func_array(Array, Array)","#11 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))","#12 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(102): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))","#13 \/home\/vagrant\/Code\/Api\/vendor\/dingo\/api\/src\/Http\/Middleware\/Request.php(124): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))","#14 \/home\/vagrant\/Code\/Api\/vendor\/dingo\/api\/src\/Http\/Middleware\/Request.php(100): Dingo\\Api\\Http\\Middleware\\Request->sendRequestThroughRouter(Object(Dingo\\Api\\Http\\Request))","#15 [internal function]: Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))","#16 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(124): call_user_func_array(Array, Array)","#17 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))","#18 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\\Http\\Request))","#19 [internal function]: Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))","#20 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Pipeline\/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\\Http\\Request))","#21 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Kernel.php(132): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))","#22 \/home\/vagrant\/Code\/Api\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Http\/Kernel.php(99): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))","#23 \/home\/vagrant\/Code\/Api\/public\/index.php(53): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))","#24 {main}"]

Yeah, I've sent a pull request to fix this. Hopefully it will get merged soon.

Ah, gotcha, I see now.

I want to look at something else first.

Also if you're experiencing issues with the latest commits, which I know a lot are, just roll back to beta3 for the time being.

Trying to fix one important issue and making about 10 others at the moment. Give me a little more time to sort it though.

When you get the chance please pull in the latest commit and let me know if this is resolved. I'm now manually setting the application routes back on the container to trigger the rebinding event so the routes on the generator will remain the same.

The new update broke all my laravel links :(

 { message: '404 Not Found',
  status_code: 404,........

Going to need more info @KhaledSMQ? You've updated to the latest commits?

@jasonlewis

simply routing are not found even the wildcard returning undefined ... any idea or update soon to fix routing issue ?

As @jasonlewis response above ,already fixed. Just pull latest commits. Im not tested yet.

@hakanersu
i already pulled it yesterday. i think this issue comes with the latest commit but am not sure.

i can see the correct routing in console ( php artisan route:list , and php artisan api:routes),
this issue make me confused some of routes are working and new routes are not returning 404

After stopping the Package service providers the laravel routes back to work normally , the package make me feel like trying to create second laravel inside laravel.

@KhaledSMQ, are you using Laravel 5.1 or 5.2? So you're saying that Laravel routes are not working?

How have you configured the API? Do you have API_DOMAIN and API_PREFIX set or just one of them?

@jasonlewis

here is my ENV file

API_STANDARDS_TREE="vnd"
API_SUBTYPE="khaled"
API_PREFIX="api"
API_DOMAIN="api.myserver.com"
API_VERSION="v1"
API_NAME="My API"
API_CONDITIONAL_REQUEST=false
API_STRICT=false
API_DEFAULT_FORMAT=json
API_DEBUG=true

You should not be setting a prefix and a domain. One or the other.

@jasonlewis
Great it's working now
i have already tried to remove the prefix but didn't work then i return the prefix and remove the domain it's working fine now

i think the problem was from the domain API_DOMAIN

I have this error too after lastest commit and from your conversations i noticed,
when i set API_DOMAIN laravel routes are returned undefined , but it works good with setting API_PREFIX .
@jasonlewis is it fixeable or i found other solution ?

@amirhabibzadeh API_DOMAIN should ONLY be used when you're using Laravel and have a virtual host configured for the sub-domain, for example, api.yourapp.com.

If you're using API_DOMAIN then API_PREFIX should be null.

If you're API is served from yourapp.com/api then you'll want to be using API_PREFIX.

@jasonlewis thanks for reply ,
I update dingo with composer 2 minutes ago .
I have virtualhost api.mysite.com
And not use api prefix.

NOW
all routes not working

my config :laravel 5.1
this is my .env

API_STANDARDS_TREE=x
API_SUBTYPE=evand
API_PREFIX=null
API_DOMAIN=api.evand.dev
API_VERSION=v1
API_NAME=Evand API
API_CONDITIONAL_REQUEST=false
API_STRICT=false
API_DEBUG=true

this my routes.php
some section

//for laravel routes
Route::get('/', function(){
   return 'It's NOT works :(';
});

//for api routes
$api = app(Dingo\Api\Routing\Router::class);
$api->version('v1', ['namespace' => 'Evand\Http\Controllers\V1'], function ($api) {
    $api->get('/apiroute', function(){
        return "it works :)";
    });
});

but same code with oldest relase of dingo works fine .

There must be something else missing @amirhabibzadeh.

Can you open a new issue with as much info as you can, include errors and stack traces. Even better would be pushing the broken code up so I can pull it down and test it out.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fengerwoo picture fengerwoo  路  4Comments

lloricode picture lloricode  路  3Comments

pedrolari picture pedrolari  路  3Comments

Kirtsen picture Kirtsen  路  3Comments

yaoshanliang picture yaoshanliang  路  4Comments