1

编辑:请参阅下文了解我当前的问题。顶部是我已经解决但有些相关的先前问题

我需要在它实际到达那里之前修改传递给我的控制器的输入值。我正在构建一个 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

我现在遇到的问题是:Router 在过滤器之后尝试去的路径,包含.json来自输​​入 URI。我见过的解决此问题的唯一选择是替换Input::replace($new_input)

$new_path = str_replace('.json', '', Request::path());
Redirect::to($new_path)->withInput($new_input);

然而,这会导致 2 个问题。首先,我无法通过请求重定向它POST- 它始终是一个GET请求。其次,传入的数据正在被刷新到会话中——我宁愿通过Input类使用它,就像使用Input::replace().

关于如何解决这个问题的任何建议?

4

1 回答 1

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. 这些文件的其余部分只是对我的数据模型的验证以及运行这两个命令的结果。

我没有拆分提交,因为我已经工作了几个小时,只是想要它。

我将希望扩展后缀工具以允许后缀数组,以便任何匹配都可以进行。例如,

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
  • method是定义在ApiController
  • {/{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项目。

于 2013-07-17T08:17:44.383 回答