編輯:請參閱下面的我當前的問題。最上面的部分是我已經解決但前面的問題,但有點相關如何刪除/註冊Laravel路徑後綴?
我需要修改傳遞給我的控制器的輸入值,然後才真正到達那裏。我正在構建一個Web應用程序,我希望能夠支持多種請求輸入類型(最初是JSON和XML)。我希望能夠在它進入我的寧靜控制器之前捕捉輸入,並將其修改爲適當的StdClass對象。
在我的生活中,我無法弄清楚如何截取和修改輸入。幫幫我?
例如,我希望能有過濾器這樣的:
Route::filter('json', function()
{
//modify input here into common PHP object format
});
Route::filter('xml', function()
{
//modify input here into common PHP object format
});
Route::filter('other', function()
{
//modify input here into common PHP object format
});
Route::when('*.json', 'json'); //Any route with '.json' appended uses json filter
Route::when('*.xml', 'xml'); //Any route with '.json' appended uses json filter
Route::when('*.other', 'other'); //Any route with '.json' appended uses json filter
現在我只是做了Input::isJson()
檢查在我的控制器功能,然後下面的代碼 - 注意這是我的代碼的一個簡化。
$data = Input::all();
$objs = array();
foreach($data as $key => $content)
{
$objs[$key] = json_decode($content);
}
編輯:其實我已經解決了這個,但現在有另一個問題。下面是我如何解決它:
Route::filter('json', function()
{
$new_input = array();
if (Input::isJson())
{
foreach(Input::all() as $key => $content)
{
//Do any input modification needed here
//Save it in $new_input
}
Input::replace($new_input);
}
else
{
return "Input provided was not JSON";
}
});
Route::when('*.json', 'json'); //Any route with '.json' appended uses json filter
我現在的問題是這樣的:路由器將嘗試去過濾後的路徑,包括從輸入URI .json
。我已經看到了解決這個唯一的選擇就是與
$new_path = str_replace('.json', '', Request::path());
Redirect::to($new_path)->withInput($new_input);
然而,這導致了2個問題,以取代Input::replace($new_input)
。首先我不能讓它重定向一個POST
請求 - 它總是一個GET
請求。其次,正在傳遞的數據正在閃現到會議中 - 我寧願通過Input
課程提供數據,因爲它將與Input::replace()
一起提供。
關於如何解決這個問題的任何建議?