Php Laravel路线:如何动态使用路线

Php Laravel路线:如何动态使用路线,php,laravel,routes,controller,Php,Laravel,Routes,Controller,我有两个相同的网址。如何动态重定向到正确的路由 我使用了->where(),但我明白,它只是验证变量,而不是过滤路径 Route::match(['get', 'post'], '/{model}/{method}/{id?}', [CrudController::class, 'index']) ->defaults('id', '') ->defaults('config', $config) ->where('model', 'Task');

我有两个相同的网址。如何动态重定向到正确的路由

我使用了->where(),但我明白,它只是验证变量,而不是过滤路径

 Route::match(['get', 'post'], '/{model}/{method}/{id?}', [CrudController::class, 'index'])
   ->defaults('id', '')
   ->defaults('config', $config)
   ->where('model', 'Task');

 
 //Hide headers, for users
 Route::match(['get', 'post'], '/{model}/{method}/{id?}', [CrudController::class, 'index'])
   ->defaults('id', '')
   ->defaults('config', $config_user)
   ->where('model', 'User');
我用肮脏的方式解决了这个问题(Laravel 8.0),希望在未来的版本中,Laravel将提供路由过滤,通过变量

 $model = request()->segment(1);
 
 if (in_array($model, $config['allow_models']))       
   Route::match(['get', 'post'], '/{model}/{method}/{id?}', [CrudController::class, 'index'])
     ->defaults('id', '')
     ->defaults('config', $config);

 else if (in_array($model, $config_user['allow_models']))
   //Hide headers, for users
   Route::match(['get', 'post'], '/{model}/{method}/{id?}', [CrudController::class, 'index'])
     ->defaults('id', '')
     ->defaults('config', $config_user);

这是不可能的,因为两者具有相同的图案样式。添加一个区分符,如
task/{model}/{method}/{id?}
user/{model}/{method}/{id?}
。这也有助于避免与将来的路由混淆。为什么您要反对框架?您的意思是,为什么我不为{model}使用静态部分?因为,在最终的代码中,这意味着要复制/粘贴20个块。