Api: Global API version namespace?

Created on 14 Nov 2016  路  3Comments  路  Source: dingo/api

It's kind of pretty cumbersome to add the full name of a controller such as:

$api->version('v1', function($api) {
    $api->get('user/{username}', 'App\Http\Controllers\v1\UserController@getUser');
});

Why can't it be something like this:

$api->version(['version' => 'v1, 'namespace' => 'App\Http\Controllers\v1\'], function($api) {
    $api->get('user/{username}', 'UserController@getUser');
});

I saw nothing in the docs mentioning that issue, but its much neater to have the namespace set this way

Most helpful comment

You can, sort of:

$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
    $api->group([
        'namespace' => 'App\Http\Controllers\v1',
    ], function ($api) {

        $api->get('foo', 'BarController@baz');

    };
};

All 3 comments

You can, sort of:

$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
    $api->group([
        'namespace' => 'App\Http\Controllers\v1',
    ], function ($api) {

        $api->get('foo', 'BarController@baz');

    };
};

That should have been noted in the wiki, it makes much more sense like that.

you can also do this in your route service provider:

````php
/**
* Define the routes for the application.
*
* @param Illuminate\Routing\Router $router
* @param Dingo\Api\Routing\Router $apiRouter
* @return void
*/
public function map(Router $router, DingoRouter $apiRouter)
{
$router->group(['namespace' => $this->namespace], function ($router) {
require app_path('Http/routes.php');
});

    $apiRouter->group(['namespace' => $this->namespace], function ($router) {
        require app_path('Http/routes.php');
    });
}

````

Was this page helpful?
0 / 5 - 0 ratings