0

I am running into an issue with internal post requests using latest Laravel 5.2 and Dingo dev/master (commit 904d4e4e). Get requests work fine, but I keep getting a 'missing argument 1' error exception when I attempt any post request from anywhere (routes.php, and controllers).

My App Controllers all extend my Base Controller, which is set up as follows:

use Dingo\Api\Dispatcher;
use Dingo\Api\Routing\Helpers;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    use Helpers;

    public $dispatcher;

    public function __construct(Dispatcher $dispatcher)
    {
        $this->dispatcher = $dispatcher;
    }

    // My global functions (irrelevant)...
}

Example of admin subdomain 'modelExample' controller:

namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\ModelExample;
use Dingo\Api\Dispatcher;
use App\Http\Controllers\BaseController;

class AdminModelExampleController extends BaseController
{

    public function __construct(Dispatcher $dispatcher)
    {
        parent::__construct($dispatcher);
    }

    /**
     * Display a listing of ModelExamples.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $modelExamples = $this->dispatcher->get('model-example');

        return view('admin.model-examples.index')->with(['model_examples' => $modelExamples]);
    }

    /**
     * Store a newly created ModelExample in the DB.
     *
     * @param $request
     * @return array
     */
    public function store($request)
    {
        $data = $request->all();

        $modelExample = $this->dispatcher->post('model-example', $data);

        return $modelExample;
    }

}

Example of 'modelExample' API controller:

namespace App\Api\Controllers;

use App\Http\Requests;
use App\ModelExample;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;

class ApiAddMediaMethodController extends BaseController
{

    /**
     * Return all ModelExamples.
     *
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function index()
    {
        return ModelExample::all();
    }

    /**
     * Store a newly created ModelExample in the DB.
     *
     * @param $request
     * @return array
     */
    public function store($request)
    {
        // I would obviously do stuff here, but for testing purposes, we'll just return...

        return $request;
    }
}

My routes.php

/*
|--------------------------------------------------------------------------
| Dingo generated router for API Routes
|--------------------------------------------------------------------------
*/
$api = app('Dingo\Api\Routing\Router');
$dispatcher = app('Dingo\Api\Dispatcher');

$api->version('v1', function ($api) {

    // Set the namespace for the API_v1-specific route controllers
    $api->group(['namespace' => 'App\Api\Controllers'], function ($api) {

        //Model Example routes
        $api->get('model-example', [
            'as' => 'model-example.get',
            'uses' => 'ApiModelExampleController@index'
        ]);

        $api->post('model-example', [
            'as' => 'model-example.post',
            'uses' => 'ApiModelExampleController@store'
        ]);
    });
});

/*
|--------------------------------------------------------------------------
| Application (non-API) Web Routes
|--------------------------------------------------------------------------
*/
Route::group(['middleware' => 'web'], function () {

    // Bunch of regular web app routes (irrelevant)

    /*
    |--------------------------------------------------------------------------
    | ADMIN Subdomain Routes
    |--------------------------------------------------------------------------
    */
    Route::group(['domain' => 'admin' . env('SESSION_DOMAIN')], function () {
        // Set the namespace for the Admin Subdomain route controllers
        Route::group(['namespace' => 'Admin'], function () {

            Route::group(['middleware' => 'admin'], function(){
                // Model Example routes
                Route::resource('model-example', 'AdminModelExampleController');
            }

        }
    }
}

No matter what I use in the store method on the Admin Controller, I get the following error:

Missing argument 1 for App\Api\Controllers\ApiModelExampleController::store()

I have tried:

$modelExample = $this->dispatcher->with($data)->post('model-example');
$modelExample = $this->dispatcher->post('model-example', ['hard' => 'coded', 'just' => 'in case']);
$modelExample = $this->api->post('model-example', $data);
$modelExample = $this->api->with($data)->post('model-example');
$modelExample = $this->api->post('model-example', ['hard' => 'coded', 'just' => 'in case']);

I have also tried directly from the routes file with test endpoints, I have turned off all middleware... no matter what I do, the payload array is not being recognized.

Is this a Laravel 5.2 vs 5.1 issue? Am I missing something completely obvious (been known to happen ...often)? How can I resolve this? Much thanks in advance! :-)

DonnaJo
  • 538
  • 6
  • 15

1 Answers1

2

SOLVED.

It was my bad.

The resolution (for future searchers) was actually quite simple.

The data that is passed along with the Post request MUST be a Request instance (i.e., Illuminate/Http, or Form Request). A simple array or non-request object will not work. I hope this saves someone else from pulling their hair out. :-)

DonnaJo
  • 538
  • 6
  • 15
  • Hello can you please post a working example, I have the same issue but cannot seem to understand your explanation. – user3718908x100 May 02 '16 at 16:17
  • You can take a look at my repo here: https://github.com/dojorob76/laravel-5-2-self-consuming-api for working examples. App/Http/Controllers/BaseController handles all the Dingo dispatcher stuff, and every other controller (in Http/Controllers, and Http/Controllers/Admin) extend that. Hope it helps! – DonnaJo May 02 '16 at 22:35