我在使用最新的 Laravel 5.2 和 Dingo dev/master(提交 904d4e4e)时遇到了内部发布请求的问题。获取请求工作正常,但是当我尝试从任何地方(routes.php 和控制器)进行任何发布请求时,我不断收到“缺少参数 1”错误异常。
我的 App Controller 都扩展了我的 Base Controller,设置如下:
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;
}
}
我的路线.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');
}
}
}
}
无论我在管理控制器上的 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']);
我也直接从带有测试端点的路由文件中尝试过,我已经关闭了所有中间件......无论我做什么,都无法识别有效负载数组。
这是 Laravel 5.2 与 5.1 的问题吗?我是否遗漏了一些完全明显的事情(众所周知……经常发生)?我该如何解决这个问题?非常感谢提前!:-)