slim framework - 将路由分类到不同的文件中?

时间:2015-04-04 17:04:29

标签: php routes slim

如何在Slim Framework中对路线进行分类?

我的公共目录中有这些基本/前沿路线,

$app->get('/about', function () use ($app, $log) {
    echo "<h1>About ", $app->config('name'), "</h1>";
});

$app->get('/admin', function () use ($app, $log) {
    echo 'admin area';
    require dirname(__FILE__) . '/../src/Admin/index.php';
});

// To generic
$app->get('/', function () use ($app, $log) {
    echo '<h1>Welcome to ', $app->config('name'), '</h1>';
});

// Important: run the app ;)
$app->run();

正如您所看到的那样,当路线在/admin时,它会在管理目录中加载index.php

管理员/ index.php的,

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

        // Contact group
        $app->group('/contact', function () use ($app) {

            // Get contact with ID
            $app->get('/contacts', function () {
                echo 'list of contacts';
            });

            // Get contact with ID
            $app->get('/contacts/:id', function ($id) {
                echo 'get contact of ' . $id;
            });

            // Update contact with ID
            $app->put('/contacts/:id', function ($id) {
                echo 'update contact of ' . $id;
            });

            // Delete contact with ID
            $app->delete('/contacts/:id', function ($id) {
                echo 'delete contact of ' . $id;
            });

        });

        // Article group
        $app->group('/article', function () use ($app) {

            // Get article with ID
            $app->get('/articles', function () {
                echo 'list of articles';
            });

            // Get article with ID
            $app->get('/articles/:id', function ($id) {
                echo 'get article of ' . $id;
            });

            // Update article with ID
            $app->put('/articles/:id', function ($id) {
                echo 'update article of ' . $id;
            });

            // Delete article with ID
            $app->delete('/articles/:id', function ($id) {
                echo 'delete contact of ' . $id;
            });

        });

    });

假设我的管理区域中有文章,联系人等模块。在每个模块中,我都有get,put,delete路由。所以我按照Admin/index.php中的模块对它们进行分组。但是当我在我的网址上请求这些模块时,我找不到404页面,例如http://{localhost}/admin/contact我应该在我的浏览器上获得list of articles,但我得到了404。

如果我将分组的路线放在public/index.php中,我就不会遇到这个问题,但我不想让这个索引变得杂乱,因为我有更多模块添加。我更喜欢将路由拆分为不同的索引。

因此,可以拆分分组不同位置(目录)中的不同index.php。

或者这可能不是我应该如何在Slim中做到这一点?如果是这样,Slim解决这个问题的方法是什么?

1 个答案:

答案 0 :(得分:1)

在您的代码中/admin/contact是处理联系人“GET/PUT/DELETE

的路由的组
// see here v 
    $app->group('/contact', function () use ($app) {}):

如果您想拥有包含此网址段的路线,则应将group替换为get

也许你可以删除admin

中的群组
 // API group
    $app->group('/admin', function () use ($app) {

        // Contact group
    //  $app->group('/contact', function () use ($app) {

            // Get All Contacts
            $app->get('/contacts', function () {
                echo 'list of contacts';
            });

            // Get contact with ID
            $app->get('/contacts/:id', function ($id) {
                echo 'get contact of ' . $id;
            });

            // Update contact with ID
            $app->put('/contacts/:id', function ($id) {
                echo 'update contact of ' . $id;
            });

            // Delete contact with ID
            $app->delete('/contacts/:id', function ($id) {
                echo 'delete contact of ' . $id;
            });

     // });
    });

然后您可以按照以下方式访问路线

Get All -> admin/contacts
Get One -> admin/contacts/:id
Update ->  admin/contacts/:id