Php 按参数值限制对路由的访问
我的路线如下:Php 按参数值限制对路由的访问,php,laravel,routing,laravel-routing,Php,Laravel,Routing,Laravel Routing,我的路线如下: Route::group([ 'prefix' => 'reports' ], function () { Route::get('/points/{product_name}', ['uses' => 'MyController@get'])->where('product_name', ['product1', 'product2','product3']) }); 因此,我想限制对此端点id的访问。产品名称是product1,pr
Route::group([
'prefix' => 'reports'
], function () {
Route::get('/points/{product_name}', ['uses' => 'MyController@get'])->where('product_name', ['product1', 'product2','product3'])
});
因此,我想限制对此端点id的访问。产品名称是product1
,product2
,product3
。但是对于where
子句,我只能看到使用正则表达式或单个值进行检查
当我使用像['product1','product2','product3']
这样的数组时,它抛出了一个错误“消息”:“product\u name”的路由要求必须是字符串。”
我怎样才能解决这个问题 您可以使用发送和接收
发送示例:
$array_send = ['product1', 'product2', 'product3'];
$imp_send = implode(",", $aray_send);
// return: product1,product2,produtct3
$array_receive = explode(',', '$product_name');
// return again array
接收示例:
$array_send = ['product1', 'product2', 'product3'];
$imp_send = implode(",", $aray_send);
// return: product1,product2,produtct3
$array_receive = explode(',', '$product_name');
// return again array
对于laravel的路线,您可以这样使用:
where('product_name','product1 | product2 | product3'])
或使用以下数组:
where('product_name',inclode('124;“,['product1','product2','product3'))
根据需要,您可以在路由实例上使用where方法约束路由参数的格式。where方法接受参数的名称和一个正则表达式,该正则表达式定义如何约束参数。因此,您应该按照以下步骤操作:
$allowedParams = implode('|',['product1','product2','product3']);
// it will return you a string as 'product1|product2|product3'
Route::get('/points/{product_name}', ['uses' => 'MyController@get'])
->where('product_name', $allowedParams);
内爆(',',['product1','product2','product3']);显示在何处执行操作和数组的代码。编辑问题我认为此逻辑应该是控制器的一部分,而不是路由何处使用此?在路由中的
where
子句中?@HappyCoder您可以使用内爆传递路由,并使用explode and get sent数组按时接收。@HappyCoder现在我明白了,路由中的where仅用于创建约束,传递数组,并且需要关联:例如->where(数组('id'=>')[0-9]+“,”名称“=>”[a-z]+))