繁体   English   中英

Tonic.PHP关于具有“ / resource”和“ / resource / id” URL的

[英]Tonic.PHP on having “/resource” and “/resource/id” URL's

我正在尝试获取URL“ / videoGame”以运行“ listAllVideoGames”方法,以及“ / videoGame /#”(其中#是数字)以运行“ getVideoGame”方法。 使用“ @priority”注解更改优先级我可以同时调用这两个URL,但是找不到找到我所描述的方法的方法。

/**
 * @uri /videoGame
 * @uri /videoGame/:id
 */
class VideoGame extends Resource{

    protected function getDao(){
        return new VideoGameDao();
    }

    /**
     * @uri /videoGame
     * @json
     * @provides application/json
     * @method GET
     */
    public function listAllVideoGames(){
        return new Response(Response::OK,$this->dao->getAllVideoGames());
    }

    /**
     * @uri /videoGame/:id
     * @json
     * @provides application/json
     * @method GET
     */
    public function getVideoGame($id){
        $vg = $this->dao->getVideoGame($id);
        if ($vg){
            return new Response(Response::OK,$vg);
        }else{
            throw new NotFoundException();
        }
    }
}

我发现做到这一点的唯一方法是为GET调用创建一种调度程序,如下所示:

/**
 * @uri /videoGame
 * @uri /videoGame/:id
 */
class VideoGame extends Resource{

    protected function getDao(){
        return new VideoGameDao();
    }

    /**
     * @uri /videoGame/:id
     * @provides application/json
     * @method GET
     */
    public function getVideoGames($id = 0){
        if (is_numeric($id) && $id > 0){
            return $this->getVideoGame($id);
        }else{
            return $this->getAllVideoGames();
        }
    }

    private function getVideoGame($id){
        $vg = $this->dao->getVideoGame($id);
        if ($vg){
            return new Response(Response::OK,$vg);
        }else{
            throw new NotFoundException();
        }
    }

    public function getAllVideoGames(){
        return new Response(Response::OK,$this->dao->getAllVideoGames());
    }

暂无
暂无

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

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