I want all requests for which there is no route to be checked for the length of the characters after the domain. If the number is less than 5, a certain controller with a certain action should be called and if it is >=5, another controller and action should be called.
Sample: 1. https://test.com/abcd should be use this controller/action controller: color action: colortype
2. https://test.com/aabbc should use this controller/action controler: car action: type
3. https://test.com/infos should call infos controller, because there is a route saved into web.php or api.php
How can I realize it? Thanks for help.
CodePudding user response:
You can use controller redirection
for it
In web.php
:
Route::get('/{text}', function ($text){
if ($text == 'infos'){
return redirect()->action([InfosController::class, 'infoAction']);
}
if (strlen($text) >= 5){
return redirect()->action([ActionController::class, 'carAction']);
}
return redirect()->action([ActionController::class, 'colorAction']);
});
Or you can offload it to middleware class
CodePudding user response:
Another Way with Regex:
Route::get('infos',[InfosController::class, 'infoAction']);
Route::get('{lessThan5}', [InfosController::class, 'colorAction'])
->where('lessThan5', '^(.{1,4})');
Route::get('{greatherThan5}', [InfosController::class, 'typeAction'])
->where('greatherThan5', '^(.{5,})');