SlimPhp函数声明中的问题

时间:2015-12-28 11:46:30

标签: php rest slim

自从我开始发现苗条并遇到问题以来,我不知道如何寻找解决方案,因为它很奇怪。 基本上,如果我声明一个在路由调用另一个函数后从路由调用的函数,则不执行第一个函数。

API组

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

    //print all route
    $app->get('/all',function () use($app){
        echoRoutes();
    });
    // Library group
    $app->group('/themer', function () use ($app) {

         //get number of subscribed themer
        $app->get('/count','allowed',function (){
            echo "ciao";
        });
        //get information about the themer selected
        $app->get('/:id','getThemer'); //AFTER THIS ROUTES /ciao AND /themes NOT WORK

         $app->get('/ciao',function () use($app){
        echoRoutes();
    });

     // Get book with ID
        $app->get('/themes', function () use ($app) {
            $articles = R::findAll('users'); 
            $app->response()->header('Content-Type', 'application/json');
            echo json_encode(R::exportAll($articles));
        });

        //get number of submitted theme by themer
        //$app->get('/:id/themes','getSubmitedThemeById');


        //get information about selected theme
        //$app->get('/:id/themes/:theme','getThemeById');

        $app->get('/themes/:id/', function ($id) {
            $articles = R::find("users","id = ?",[$id]);
            echo json_encode(R::exportAll($articles));
        });

    });

});

带功能的外部文件

//external file with function
function  getThemer($key,$id) { 
   $themer = R::find("themers","id = ?",[$id]);
   echo json_encode(R::exportAll($themer));
   return true;
}

function countThemer(){
    echo "count";
    $count =  R::exec( 'SELECT COUNT(id) FROM themers' );
    echo $count;
}

function allowed($key){
    $app = \Slim\Slim::getInstance();
    $params = $app->router()->getCurrentRoute()->getParams();
    if(!($params["key"]=="giulio"))
        $app->redirect ("http://google.com");

}

路线 index.php / api / giulio / themer / 1 之后调用getThemer并使用路径 index.php / api / giulio / themer / ciao index.php / api / giulio / themer / themes 无效

我事先感谢您提供可能的帮助 欢迎批评或评论一般外观中的代码

1 个答案:

答案 0 :(得分:1)

更改路线的顺序:

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

    //print all route
    $app->get('/all',function () use($app){
        echoRoutes();
    });
    // Library group
    $app->group('/themer', function () use ($app) {

         //get number of subscribed themer
        $app->get('/count','allowed',function (){
            echo "ciao";
        });

        $app->get('/ciao',function () use($app){
            echoRoutes();
        });

        // Get book with ID
        $app->get('/themes', function () use ($app) {
            $articles = R::findAll('users');
            $app->response()->header('Content-Type', 'application/json');
            echo json_encode(R::exportAll($articles));
        });

        //get number of submitted theme by themer
        //$app->get('/:id/themes','getSubmitedThemeById');


        //get information about selected theme
        //$app->get('/:id/themes/:theme','getThemeById');

        $app->get('/themes/:id/', function ($id) {
            $articles = R::find("users","id = ?",[$id]);
            echo json_encode(R::exportAll($articles));
        });

        //get information about the themer selected
        $app->get('/:id','getThemer');

    });

});