Filter 如何删除/注册Laravel路线上的后缀?
编辑:有关我当前的问题,请参见下文。上半部分是我之前解决的问题,但有点相关 我需要在控制器真正到达之前修改传递给它的输入值。我正在构建一个web应用程序,希望能够支持多种请求输入类型(最初是JSON和XML)。我希望能够在输入进入restful控制器之前捕获它,并将其修改为适当的StdClass对象 就我的一生而言,我无法想出如何截取和修改输入。帮忙 例如,我希望能够有这样的过滤器:Filter 如何删除/注册Laravel路线上的后缀?,filter,routes,laravel,Filter,Routes,Laravel,编辑:有关我当前的问题,请参见下文。上半部分是我之前解决的问题,但有点相关 我需要在控制器真正到达之前修改传递给它的输入值。我正在构建一个web应用程序,希望能够支持多种请求输入类型(最初是JSON和XML)。我希望能够在输入进入restful控制器之前捕获它,并将其修改为适当的StdClass对象 就我的一生而言,我无法想出如何截取和修改输入。帮忙 例如,我希望能够有这样的过滤器: Route::filter('json', function() { //modify input he
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
。我看到的解决这个问题的唯一方法是将Input::replace($new\u Input)
替换为
$new_path = str_replace('.json', '', Request::path());
Redirect::to($new_path)->withInput($new_input);
然而,这导致了两个问题。首先,我不能用POST
请求让它重定向-它总是get
请求。第二,传入的数据正在被闪存到会话中——我更希望通过Input
类获得它,就像Input::replace()
一样
关于如何解决这个问题,有什么建议吗?我也设法解决了第二个问题——但这涉及到很多额外的工作和摸索。。。我不确定这是否是最好的解决方案,但它允许为路径添加后缀,类似于您为它们添加前缀的方式 下面是我如何解决它的github提交: 特别是,您应该看到:
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 dumpautoload
和php-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}`
其中:
是域的基本urlbase\u url
是在method
ApiController
是一系列多达5个可选变量,在向{/{v1?}/{v2?}/{v3?}/{v4?}/{v5?}
路由::控制器()注册控制器时添加这些变量。
是传递给suffix
Route::group()
localhost
是基本url,可用的方法有getMethod1($str1=null,$str2=null)
和postMethod2()
):
request toGET
localhost/api/method1.json
request toGET
localhost/api/method1.xml
request toGET
localhost/api/method1some\u other\u url\u suffix
request toPOST
localhost/api/method2.json
request toPOST
localhost/api/method2.xml
request toPOST
localhost/api/method2some\u other\u url\u后缀
request toGET
localhost/api/method1/hello/world.json
request toGET
localhost/api/method1/hello/world.xml
request toGET
localhost/api/method1/hello/worldsome\u other\u url\u后缀
$str1='hello'
和$str2='world'
作为参数传递到getMethod1
编辑:允许多个后缀的更改相当简单。位于下面的提交(请确保您获得了两个提交更改以使其正常工作):
最后,我也希望将此提交到项目中。我也设法解决了第二个问题-但这涉及到大量额外的工作和四处摸索。。。我不确定这是否是最好的解决方案,但它允许为路径添加后缀,类似于您为它们添加前缀的方式 下面是我如何解决它的github提交: 特别是,您应该看到:
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 dumpautoload
和php-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}`
其中:
是域的基本urlbase\u url
是在method
ApiController
是一系列多达5个可选变量,在向{/{v1?}/{v2?}/{v3?}/{v4?}/{v5?}
路由::控制器()注册控制器时添加这些变量。
是传递给suffix
Route::group()
localhost
是基本url,可用的方法有getMethod1($str1=null,$str2=null)
和postMethod2()
):
request toGET
localhost/api/method1.json
request toGET
localhost/a