Silex添加路由文件的所有路由到控制器


Silex add Routing file with all the routes to controllers

我使用silex来创建我的API REST。在一个例子中,我找到了一种方法来创建路由

$api = $this->app["controllers_factory"];
$api->get('/notes', "notes.controller:getAll");
$api->get('/notes/{id}', "notes.controller:getOne");
$api->post('/notes', "notes.controller:save");
$api->put('/notes/{id}', "notes.controller:update");
$api->delete('/notes/{id}', "notes.controller:delete");

我正在寻找一种方法,包括一个数组与所有的路由和创建和实例上我的应用程序引导文件。你知道吗?

    应用<<li>扩展Silex '/gh>
  1. 添加只根据参数添加路由的功能
  2. 在带有所需参数的元素数组上运行foreach

use Silex'Application as SilexApplication;
class Application extends SilexApplication
{
    public function addRoute($method, $route, $class, $callback)
    {
        $this->$method($route, array($class, $callback));
    }
    public function addRouteStatic($method, $route, $callback)
    {
        $this->$method($route, $callback);
    }
    public function addRoutes($routes)
    {
        foreach ($routes as $route) {
            $this->addRoute(
                $route['method'],
                $route['route'],
                $route['class'],
                $route['callback']
            );
        }
    }
}
$app = new Application();
$app->addRoute('get', '/notes', 'My'Namespace'Note', 'getAllNotes');
$app->addRouteStatic('get', '/notes', 'My'Namespace'Note::getAllNotesStatic');