I have a regex that matches any slug likes this:
dan-huyen
dan-huyen-123
123-dan-huyen
and not match the following:
123
123456
Here is the regex:
$theregex = '/^(?=.*[a-z])[a-z0-9-]+$/';
The regex has been tested, and I make sure that it worked perfectly. Demo test: https://regex101.com/r/fQ1iD5/17
Then I use this regex into my Micro App to catch any request. This is one of my routing:
$v1->get("/{class:$people_restricted}/{slug:(?=.*[a-z])[a-z0-9-]+}", 'getOneBySlug');
This is the function that handled the request if routing matched:
public function getOneBySlug($class, $slug) {
$object = get_result($class, 'findFirstBySlug', $slug);
if ($object) {
$clean_obj = $this->filter($class, $object);
return array('status' => 'ok', 'data' => $clean_obj);
} else {
$messages = array();
$messages[] = 'Object not found';
return array('status' => 'error', 'messages' => $messages);
}
}
I've tried the routing above and it does match the request like this:
/composer/dan-huyen?fields=id,title,content,image,views&extra_fields={song:fields=id,title,slug},{document:fields=id,title,slug}
But instead of return the right slug in function getOneBySlug ($slug = 'dan-huyen'), it always returns the value '2' of $slug variable ($slug = 2); The $class variable return the right value ('composer')
So may I miss something where?
Update: I changed my question title from "How to route use complex regex" to "Routing problem, always return a fixed value in handle" because I missed something on cheking the result.