4

I am using dingo/api for a project and all of my routes work other than 1.

routes.php

    $api = app('api.router');
    $api->version('v1',['prefix' => 'api'], function ($api) {

        $api->post('users', "App\Http\Controllers\Auth\UsersController@store");
...
        });

store method :

/**
     *
     * @param  \App\Http\Requests\Auth\StoreUserRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreUserRequest $request)
    {
        if( ! $this->isAdminRequest() )
        {
            return $this->dispatch(new RegisterUserCommand($request));
        }
    }

This route throws 405 Method Not Allowed Exception.

php artisan api:routes shows that it is registered, both locally and on server. I am using POSTMAN to test my api and have all required fields. In response I do get Allow : POST.

NOTE :

  • There are other post routes that work perfectly.
  • There are other UsersController routes that work perfectly
  • I have tried removing all other routes and with just this, it still does not work.

Any help is appreciated. Thanks.

UPDATE :

Okay, it was a really silly mistake on my part. I was hitting 'domain/api/users/'. Removing the trailing '/' worked. But the thing is, locally '/' works and not on server. So keeping this question on for some explanantion maybe.

Bharat Geleda
  • 2,692
  • 1
  • 23
  • 31

1 Answers1

1

Very good question and have since ran into this myself. Your update should be in an answer so if you do that, I may remove mine. The problem for me was a trailing slash in my action e.g. /some/action/ instead of /some/action where the route expected it in the latter form.

I had assumed that Laravel would handle this optional trailing slash as a feature considering the behaviour in a local environment but this was not the case.

Jonathan
  • 10,936
  • 8
  • 64
  • 79
  • I've kept this because I was not able to find why this was happening. I have different configurations locally and on server. Maybe it's due to that. Or maybe it's because of any APACHE configuration or .htaccess. I was hoping to find what can effect and cause this behaviour. – Bharat Geleda Jul 29 '16 at 18:30
  • It makes sense because your local may be running nginx and your server on Apache. The way they handle things is likely the cause of the problem – Jonathan Jul 29 '16 at 19:19
  • No it's both Apache but different versions. I'll look into this further later and update here. – Bharat Geleda Jul 29 '16 at 19:20
  • In my case it was a homestead environment which may be running nginx if not mistaken – Jonathan Jul 29 '16 at 19:39