var_dump($route);
```
+## Load routers from a `yaml` file
+
+We can define in a `yaml` file all the routes of our application. This facilitates our life when we need to *migrate*, *modify*, or later *add* new routes.
+
+The route definition should follow the example below:
+
+```yaml
+base_path: /blog
+
+routes:
+ index: [/index, someClass.indexAction, GET]
+ contact: [/contact, someClass.contactAction, GET]
+ about: [/about, someClass.aboutAction, GET]
+```
+In our **Front Controller** would have something like:
+
+```php
+<?php
+require __DIR__.'/vendor/autoload.php';
+
+use PHPRouter\RouteCollection;
+use PHPRouter\Config;
+use PHPRouter\Router;
+use PHPRouter\Route;
+
+$config = Config::loadFromFile(__DIR__.'/router.yaml');
+$router = Router::parseConfig($config);
+$router->matchCurrentRequest();
+```
+
## More information
Have a look at the example.php file or read trough the class' documentation for a better understanding on how to use this class.
}\r
],\r
"require": {\r
- "php": ">=5.3.3"\r
+ "php": ">=5.3.3",\r
+ "symfony/yaml": "@dev"\r
},\r
"require-dev": {\r
"phpunit/phpunit": "3.7.*"\r
--- /dev/null
+<?php
+namespace PHPRouter;
+
+use Symfony\Component\Yaml\Yaml;
+
+class Config
+{
+ public static function loadFromFile($yamlFile)
+ {
+ echo $yamlFile;
+ try {
+ $value = Yaml::parse(file_get_contents($yamlFile));
+ } catch (\Exception $e) {
+ echo 'Message %s'.$e->getMessage();
+ }
+ return $value;
+ }
+}
\ No newline at end of file
namespace PHPRouter;
use Exception;
+use PHPRouter\RouteCollection;
+
/**
* Routing class to match request URL's against given routes and map them to a controller action.
*/
}
return $url;
}
+
+
+ /**
+ * Create routes by array, and return a Router object
+ *
+ * @param array $config provide by Config::loadFromFile()
+ * @return Router
+ */
+ public static function parseConfig(array $config)
+ {
+ $collection = new RouteCollection();
+ foreach ($config['routes'] as $name => $route) {
+ $collection->add($name, new Route($route[0], array(
+ '_controller' => str_replace('.', '::', $route[1]),
+ 'methods' => $route[2]
+ )));
+ }
+
+ return new Router($collection);
+ }
}
\ No newline at end of file