2013-07-16 35 views
1

編輯:請參閱下面的我當前的問題。最上面的部分是我已經解決但前面的問題,但有點相關如何刪除/註冊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()一起提供。

關於如何解決這個問題的任何建議?

回答

1

我設法解決第二個問題,以及 - 但它涉及到很多額外的工作和閒逛......我不知道這是否是最好的解決辦法,但它允許類似於你會怎麼後面添加路線加上前綴。

這裏的github上提交了我如何解決它: https://github.com/pcockwell/AuToDo/commit/dd269e756156f1e316825f4da3bfdd6930bd2e85

特別的,你應該看:

app/config/app.php 
app/lib/autodo/src/Autodo/Routing/RouteCompiler.php 
app/lib/autodo/src/Autodo/Routing/Router.php 
app/lib/autodo/src/Autodo/Routing/RoutingServiceProvider.php 
app/routes.php 
composer.json 

完成上述修改後,我需要運行composer dumpautoloadphp artisan optimize。其餘的這些文件只是驗證我的數據模型以及運行這2個命令的結果。

我沒有分裂承諾,因爲我會一直在努力了幾個小時,只是想進去。

我要去看看希望延長後綴工具,允許數組後綴以便進行任何匹配。例如,

Route::group(array('suffix' => array('.json', '.xml', 'some_other_url_suffix')), function() 
{ 
    // Controller for base API function. 
    Route::controller('api', 'ApiController'); 
}); 

,這將非常接受任何調用匹配

{base_url}/api/{method}{/{v1?}/{v2?}/{v3?}/{v4?}/{v5?}?}{suffix}` 

其中:

  • base_url是域的基本URL
  • methodApiController
  • 定義的函數
  • {/{v1?}/{v2?}/{v3?}/{v4?}/{v5?}?}是一系列多達5個可選變量作爲註冊用Route::controller()
  • suffix控制器當加入的是傳遞給Route::group()

後綴數組中的值的一個本實施例中特別應該接受所有的以下(假定localhost被基本URL,和可用的方法是getMethod1($str1 = null, $str2 = null)postMethod2()):

  • GET請求localhost/api/method1.json
  • GET請求localhost/api/method1.xml
  • GET請求localhost/api/method1some_other_url_suffix
  • POST請求localhost/api/method2.json
  • POST請求localhost/api/method2.xml
  • POST請求localhost/api/method2some_other_url_suffix
  • GET請求localhost/api/method1/hello/world.json
  • GET請求localhost/api/method1/hello/world.xml
  • GET請求localhost/api/method1/hello/worldsome_other_url_suffix

最後三個請求將通過$str1 = 'hello'$str2 = 'world'getMethod1作爲參數。

編輯:允許多個後綴的更改相當容易。提交下方(請確保您同時獲得提交修改得到這個工作): https://github.com/pcockwell/AuToDo/commit/864187981a436b60868aa420f7d212aaff1d3dfe

最後,我也希望提交此爲laravel/framework項目。