我使用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");
我正在寻找一种方法来包含一个包含所有路由的数组,并在我的app bootstrap文件中创建和实例。有什么想法吗?
答案 0 :(得分:0)
示例强>
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');