1 | <?php |
||
4 | class Router { |
||
5 | |||
6 | /** |
||
7 | * Get the base from any possible url |
||
8 | * return string |
||
9 | */ |
||
10 | public function base() |
||
15 | |||
16 | /** |
||
17 | * Executes an array of routes. Should be properly formatted: |
||
18 | * ['regex'=>'class@method'] |
||
19 | * ['regex'=> ['GET','class@method']] |
||
20 | * By default index() method will be taken. |
||
21 | * No need of /^...$/ inside of regex. |
||
22 | * Returns false if route not found. |
||
23 | * |
||
24 | * @var $routes array The routes array |
||
25 | * @return string|false |
||
26 | */ |
||
27 | public function dispatch($routes) |
||
36 | |||
37 | /** |
||
38 | * Executes the request this regex and the route params. Could be a callable. |
||
39 | * @var $regex string The regex |
||
40 | * @var $route string|callable The route to be executed |
||
41 | * return string|boolean The result |
||
42 | */ |
||
43 | public function execute($regex, $callable) |
||
70 | } |
||
71 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: