2016-11-08 221 views
-2

即時通訊使用silex創建我的API REST。 在一個示例中,我發現了一個方法來創建進出口尋找一種方式來inlcude與所有路線的數組,並創建和實例在我的應用程序的引導文件的路徑Silex添加路由文件與所有路由到控制器

$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"); 

。任何想法 ?

回答

0
  1. 延伸的Silex \應用
  2. 附加功能,其與所需的PARAMS

添加基於參數的路由僅

  • 運行的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');