Suppose I have a route like this :
$api->group(['prefix' => 'Tag/{type}'], function ($api) {
});
As you can see there is a required type
parameter.
Now I want type
parameter can only one of items of an array that is defined in a config file like this :
return [
'name' => 'Tag',
'types' => [
'product' => 'product',
'user' => 'user'
]
];
Means type
can be product
or user
only. I know that I should use Regular Expression Constraints but I do not know how ?
simple!!!
create a helper file(I hope u know how to create a helper file) and make a function that return an implode version. for instance. you created a arrayconfig.php in config folder and the code inside it is
return [
'name' => 'Tag',
'types' => [
'product' => 'product',
'user' => 'user'
]
];
now in helper.php create a function
function typechecker(){
$array = config()->get('arrayConfig.types');
return implode("|",$arr);
}
you must remember you add it to composer.json file and run a command in console
composer dump-autoload
after adding helper files what is to be added in composer.json if you create a folder helpers in app and your file path is
app/Helpers/helpers.php
Your composer.json will be something like this
"autoload": {
"classmap": [
....
],
"files": [
"app/Helpers/helpers.php"
],
"psr-4": {
....
}
}
just concentrate on files. rest of the section is already there. I added all code to make it understandable
Final step in your route
$api->group(['prefix' => 'Tag/{type}'], function ($api) {})->where('type', typechecker());
Cheers!!!
N.B it's 100% workable. If it does not work, that means you did something wrong