Tonic.PHP上有“/ resource”和“/ resource / id”URL

时间:2014-03-24 02:23:42

标签: php url restful-url tonic

我试图获取网址" / 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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我发现这样做的唯一方法是为这样的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());
    }