REST简单的设置

来源:互联网 发布:战地1 知乎 编辑:程序博客网 时间:2024/06/07 00:17
最快的办法与休息是启动和运行,将几行添加到您的路线。在app /配置php文件,发现。路由器对象特性的方法称为mapResources(),用于建立一个其他的默认路由访问您的控制器。确保mapResources()之前,是需要蛋糕。“配置”。DS。的路线。php的;和其他航线覆盖的路线。如果我们想让其他的配方数据库的访问,我们做的是这样的:
//In app/Config/routes.php...Router::mapResources('recipes');Router::parseExtensions();
第一行设置一个简单的默认路由REST访问方法指定所需的结果格式(如xml、json、rss)。这些路由HTTP请求方法敏感。
HTTP formatURL.formatController action invokedGET/recipes.formatRecipesController::index()GET/recipes/123.formatRecipesController::view(123)POST/recipes.formatRecipesController::add()PUT/recipes/123.formatRecipesController::edit(123)DELETE/recipes/123.formatRecipesController::delete(123)POST/recipes/123.formatRecipesController::edit(123)

CakePHP的路由器类使用许多不同的指标来检测所使用的HTTP方法。在这里他们的偏好:
_method POST变量
的X_HTTP_METHOD_OVERRIDE
REQUEST_METHOD头
_method POST变量有助于使用浏览器作为一个REST客户机(或任何能做的文章容易)。只有设置值的_method HTTP请求方法的名称你想效仿。
一旦路由器设置其他请求映射到特定的控制器操作,我们可以继续创建逻辑控制器动作。一个基本控制器看上去是这样的:
// Controller/RecipesController.phpclass RecipesController extends AppController {    public $components = array('RequestHandler');    public function index() {        $recipes = $this->Recipe->find('all');        $this->set(array(            'recipes' => $recipes,            '_serialize' => array('recipes')        ));    }    public function view($id) {        $recipe = $this->Recipe->findById($id);        $this->set(array(            'recipe' => $recipe,            '_serialize' => array('recipe')        ));    }    public function edit($id) {        $this->Recipe->id = $id;        if ($this->Recipe->save($this->request->data)) {            $message = 'Saved';        } else {            $message = 'Error';        }        $this->set(array(            'message' => $message,            '_serialize' => array('message')        ));    }    public function delete($id) {        if ($this->Recipe->delete($id)) {            $message = 'Deleted';        } else {            $message = 'Error';        }        $this->set(array(            'message' => $message,            '_serialize' => array('message')        ));    }}

因为我们已经添加了一个叫路由器:parseExtensions(),CakePHP路由器已经准备好提供不同的视图基于不同类型的请求。因为我们正在处理其他请求,我们将使XML视图。你也可以很容易使JSON视图使用CakePHP的内置JSON和XML视图。通过使用内置的XmlView我们可以定义一个变量_serialize视图。这种特殊的视图变量用于定义视图变量XmlView应该序列化为XML。
如果我们想修改数据转换成XML之前我们不应该定义_serialize视图变量,而不是使用视图文件。我们把其他视图RecipesController内部应用程序/视图/菜谱/ xml。我们还可以使用Xml代码的Xml输出视图。这就是我们的索引视图的样子:
// app/View/Recipes/xml/index.ctp// Do some formatting and manipulation on// the $recipes array.$xml = Xml::fromArray(array('response' => $recipes));echo $xml->asXML();

当使用parseExtensions提供特定内容类型(),CakePHP自动查找视图helper类型相匹配。因为我们使用XML内容类型,没有内置的帮助,然而如果你创建一个为我们的使用在这些视图将自动被加载。
呈现XML最终会看起来是这样的:
<recipes>    <recipe id="234" created="2008-06-13" modified="2008-06-14">        <author id="23423" first_name="Billy" last_name="Bob"></author>        <comment id="245" body="Yummy yummmy"></comment>    </recipe>    <recipe id="3247" created="2008-06-15" modified="2008-06-15">        <author id="625" first_name="Nate" last_name="Johnson"></author>        <comment id="654" body="This is a comment for this tasty dish."></comment>    </recipe></recipes>

创建编辑操作的逻辑有点棘手,但也高不了多少。因为你提供的API输出XML,这是一个自然的选择接收XML作为输入。不要担心,RequestHandler和路由器类使事情更加容易。如果一个POST或PUT请求XML内容类型,然后输入通过CakePHP的XML类,运行和数据的数组表示分配给$ this - >请求- >数据。由于这个特性,并行处理XML和POST数据无缝:控制器或模型不需要改变代码。所有你需要应该在$ this - >请求- >数据。
0 0
原创粉丝点击