Laravel 5.3+ and the Dingo API router

Let me share another gotcha I found when using Laravel 5.3+ and the Dingo API package.

Sadly, Dingo is not updated (yet) to use the default new route files (routes/web.php and routes/api.php) that Laravel 5.3 implemented. So when I started to use the package, I noted that when following the steps on the wiki, the only way I could make Dingo pick up my API routes, was to place them under the routes/web.php file (or use the old app/Http/routes.php file).

I didn't like this.

So, after much googling and stackoverflowing I found this little tweak:


// inside your RouteServiceProvider
/**
 * Define the "api" routes for the application.
 *
 * These routes are typically stateless.
 *
 * @return void
 */
protected function mapApiRoutes()
{
    $api = app('Dingo\Api\Routing\Router');
    $api->version('v1', function ($api) {
        $api->group(['namespace' => $this->namespace], function ($api) {
            require base_path('routes/api.php');
        });
    });
}

And that's it! You can now write your routes in routes/api.php like so:


/* @var Dingo\Api\Routing\Router $api */

$api->group(['middleware' => 'api.auth|bindings'], function (Dingo\Api\Routing\Router $api) {

    $api->patch('users/update/{user}', 'UserController@update');

});

Look again at the above. We are using Laravel's own Route Model Binding inside our Dingo routes! That magic is granted to you by adding the bindings middleware. Also, setting the type on the closure parameter, grants your IDE autocomple power, which is always a good thing.

Have fun!