mihatt's avatar

Dingo API exception on standard web request

I was implementing API with Dingo and it was working great. Now I need to add some functionality (for user registration) that wont go trough API but normal web page. And I cant seem to find how to disable Dingo exception conversion to JSON unless I comment out dingo service provider from conf/app.php and all $api routes.

Can I live in both worlds? :)

My package routes file

Route::group(['prefix' => 'auth', 'namespace' => '\Users\Http\Controllers\Auth'], function(){
    Route::get('register', 'AuthController@getRegister');
    Route::post('register', 'AuthController@postRegister');
});


//
$api = app('Dingo\Api\Routing\Router');

$api->version('v1', function ($api) {
    $api->group(['prefix' => 'oauth', 'as' => 'oauth', 'namespace' => '\Users\Http\Controllers'],
        function ($api) {
            $api->get('/', 'OauthController@index');
            $api->post('/access_token',  ['as' => ':access_token', 'uses' => 'OauthController@token'] );

        });

});
0 likes
6 replies
Lars-Janssen's avatar

Make sure your default guard is web (in auth.php)

 'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
mihatt's avatar

Thanks @lars64 :) I checked and it is already set to guard.

Worst case scenario will be to start from scratch and copy&paste code in fresh install until I figure out what is the problematic part.

Lars-Janssen's avatar

@mihatt do not use Route::post but do it like this:

 $api->group(['prefix' => 'v1'],function($api)
    {
        $api->post('/login'                                     , 'App\Http\Controllers\api\v1\EmployeeController@login');
        $api->post('/forgot/password'                           , 'App\Http\Controllers\Auth\PasswordController@forgotPassword');
        $api->post('/reset/password'                            , 'App\Http\Controllers\Auth\PasswordController@resetEmployeePassword');
    });
mihatt's avatar

doesnt help ... routes are working fine in either case (GET is fine) but since Exception happens in POST formatting is taken from Dingo

mihatt's avatar

OK as far I have debugged Dingo actually overrides Exception handling with its own in api service provider vendor/dingo/api/src/Provider/ApiServiceProvider.php on line 277

protected function registerMiddleware()
    {
        $this->app->singleton('Dingo\Api\Http\Middleware\Request', function ($app) {
            $middleware = array_merge($app['app.middleware'], $app['config']['api.middleware']);

            return new Http\Middleware\Request($app, $app['api.exception'], $app['api.router'], $app['api.http.validator'], $middleware);
        });
mihatt's avatar
mihatt
OP
Best Answer
Level 2

Step by step debugging always works :D Found it!

I set API_DOMAIN config to same domain I was using for normal web. I had to unset domain and set API_PREFIX instead. It seems to be working now.

Thanks @lars64 for your help and time invested!

1 like

Please or to participate in this conversation.