Dingo API内部发布请求不适用于Laravel 5.2

时间:2016-03-16 14:56:45

标签: laravel-5.2 dingo-api

我使用最新的Laravel 5.2和Dingo dev / master(commit 904d4e4e)遇到了内部帖子请求的问题。请求工作正常,但我一直得到一个缺失的论点1'当我从任何地方(routes.php和控制器)尝试任何发布请求时出现错误异常。

我的应用程序控制器都扩展了我的基本控制器,其设置如下:

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)...
}

管理员子域' modelExample'控制器:

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;
    }

}

' modelExample'的示例API控制器:

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;
    }
}

我的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');
            }

        }
    }
}

无论我在Admin Controller上的store方法中使用什么,我都会收到以下错误:

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

我试过了:

$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']);

我也尝试直接从带有测试端点的routes文件,我已经关闭了所有中间件......无论我做什么,有效载荷数组都没有被识别。

这是Laravel 5.2 vs 5.1问题吗?我错过了一些完全明显的事情(众所周知......经常发生)?我该如何解决这个问题?非常感谢提前! : - )

1 个答案:

答案 0 :(得分:2)

解决。

这是我的坏事。

决议(对于未来的搜索者)实际上非常简单。

与Post请求一起传递的数据必须是Request实例(即Illuminate / Http或Form Request)。简单的数组或非请求对象将不起作用。我希望这可以节省别人的头发。 : - )