简体   繁体   English

向瘦路由器添加基本URL路径

[英]Adding a base url path to slim router

I have a bunch of routes and they all start with /api/2.01 . 我有一堆路线,它们都以/api/2.01

How can I add it once so it applies to all routes. 如何添加一次,使其适用于所有路线。 Slim Framework Base URL asks the same question, but I believe provides an outdated answer. Slim Framework Ba​​se URL会问同样的问题,但我相信提供了过时的答案。

PS. PS。 If instead of asking a new question, should I have instead somehow tagged the post which I believe is dated to be reviewed or something? 如果不问一个新问题,我是否应该以某种方式标记我认为已过审阅的帖子?

$app = new \Slim\Slim();
$app->post('/api/2.01/books', function () {
    //Create books
});
$app->get('/api/2.01/books', function () {
    //getbook
});
$app->get('/api/2.01/books/{id}', function () {
    //Get book
});
$app->delete('/api/2.01/books/{id}', function () {
    //Create book
});

If you are using Slim v2.0, you can do somtehing like: 如果您使用的是Slim v2.0,则可以执行以下操作:

// API group
$app->group('/api', function () use ($app) {

// Library group
$app->group('/library', function () use ($app) {

    // Get book with ID
    $app->get('/books/:id', function ($id) {

    });

    // Update book with ID
    $app->put('/books/:id', function ($id) {

    });

    // Delete book with ID
    $app->delete('/books/:id', function ($id) {

    });

});

as specified in the docs: http://docs.slimframework.com/routing/groups/ 如文档中所指定: http : //docs.slimframework.com/routing/groups/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM