OSDN Git Service

Creating routes from YAML files. Closed #25
authormalukenho <admin@phpse.net>
Mon, 3 Mar 2014 18:23:38 +0000 (15:23 -0300)
committermalukenho <admin@phpse.net>
Mon, 3 Mar 2014 18:23:38 +0000 (15:23 -0300)
- Now you can create a yaml file do describe routes for your application
  and load this by PHP-Router

README.md
composer.json
src/PHPRouter/Config.php [new file with mode: 0644]
src/PHPRouter/Router.php

index 0b7c885..fc38946 100755 (executable)
--- a/README.md
+++ b/README.md
@@ -44,6 +44,36 @@ $route = $router->matchCurrentRequest();
 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.
 
index bef6463..b972f8d 100755 (executable)
@@ -17,7 +17,8 @@
         }\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
diff --git a/src/PHPRouter/Config.php b/src/PHPRouter/Config.php
new file mode 100644 (file)
index 0000000..5ad9b64
--- /dev/null
@@ -0,0 +1,18 @@
+<?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
index 0259ed2..b4d3621 100755 (executable)
@@ -2,6 +2,8 @@
 namespace PHPRouter;
 
 use Exception;
+use PHPRouter\RouteCollection;
+
 /**
  * Routing class to match request URL's against given routes and map them to a controller action.
  */
@@ -140,4 +142,24 @@ class Router
         }
         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