I’m trying to create a routing system based on annotations (something like on Recess Framework).
<?php
class MyController extends ActionController {
/** !Route GET /hello/$firstname/$lastname **/
public function helloAction($firstname, $lastname) {
echo('Hello '.$firstname.' '.$lastname);
}
}
?>
If I go to http://domain.com/hello/James/Bond I get
Hello James Bond
So I have two questions:
1) Is it a good idea? Pros and cons vs centralized routing system (like Zend Framework). Maybe I don’t see problems that my arise later with this routing technique.
2) How to check for duplicate routes if there is regexp in routes
<?php
class MyController extends ActionController {
/**
*!Route GET /test/$id = {
* id: [a-z0-9]
*}
**/
public function testAction($id) {
echo($id);
}
/**
*!Route GET /test/$id = {
* id: [0-9a-z]
*}
**/
public function otherTestAction($id) {
echo($id);
}
}
?>
I get two routes: /test/[a-z0-9]/ and /test/[0-9a-z]/ and if i go to http://domain.com/test/a12/ both routes are valid.
Thanks 🙂
Cons:
If the method signature and mapping are always as related as the example you might use reflection to extract the mapping where helloAction is picked up as /hello and each method argument is a subdirectory of this in the order as they’re defined.
Then the annotation wouldn’t need to duplicate the URL, only the fact that the method is an endpoint, something like this: