Currently in my routes.php file I've got the following:
Route::get('import', 'Controller@import');
But as for the requests they'd be sent in the following way:
/import?flag=0&data={} //flag could be anything from 0-4 & data is json encoded info
/import?flag=5&data={}
/import?flag=6&data={}
These are the only 3 requests that I'd have, I'm aware that Laravel supports where
method on the route but only regular expressions but not on query parameters, is there a solution for this that I'm overlooking?
The expression method would be something like so:
Route::get('import?flag={id}&data={data}', 'Controller@importFlag0-4')->where('id', '<=', '4');
Route::get('import?flag={id}&data={data}', 'Controller@importFlag5')->where('id', '5');
Route::get('import?flag={id}&data={data}', 'Controller@importFlag6')->where('id', '6');
As mentioned by @lagbox, why would you need different routes, when you could handle all those routes conditionals in the import()
method
public function import(Request $request)
{
$flag = $request->get('flag');
$data = $request->get('data');
if ($flag <= 4) {
$this->importFlagTillFour($flag, $data)
}
if ($flag == 5) {
$this->importFlagFive($flag, $data)
}
if ($flag == 6) {
$this->importFlagSix($flag, $data)
}
}
and declare the called methods in the same class
public function importFlagTillFour($flag, $data) {}
public function importFlagFive($flag, $data) {}
public function importFlagSix($flag, $data) {}
You can use only single way
Route::any('import', 'Controller@import');
On controller write the following function.
public function import(Request $request)
{
$getData = $request->all();
$flag = $getData['flag'];
$data = $getData['data'];
$getModelData = ModelName::where('id',$flag);
}