我可以将整个 cakephp 网站转换为网络服务吗?

Can i convert whole cakephp website to web services?

嘿,我正在用 cakephp 做项目。还将有 Android 和 iPhone 的应用程序。如何将我的网络代码转换为网络服务。

我会先阅读 this

你基本上需要修改你的路线。您还需要修改(序列化)您的控制器 return.

是的,有一种方法可以在任何版本的 cakephp 的网络服务中转换完整的站点。

有以下步骤: 1. 放 "Router::parseExtensions("pdf", "json");"在路由规则之前,将此行添加到配置文件夹中的 routes.php。 2. 用以下内容覆盖 AppController 中的 "beforeRender" 和 "Redirect" 函数:

    function beforeRender() {
        if (Configure::read("debug") == 0) {
          if ($this->name == 'CakeError') {
            $this->layout = "error";
          }
        } else {
          if ($this->name == 'CakeError') {
            $this->layout = "error";
          }
        }

        if ($this->params["ext"] == "json") {

          $paging = $requests = "";

          if (isset($this->params["paging"]) && !empty($this->params["paging"])) {
            $paging = $this->params["paging"];
          }
          $this->set(compact("paging"));

          if (isset($this->request->data) && !empty($this->request->data)) {
            $requests = $this->request->data;
          }
          $this->set(compact("requests"));

          if ($this->Session->check("Message.flash") && is_array($this->Session->read("Message.flash"))) {
            foreach ($this->Session->read("Message.flash") as $key => $value) {
              $this->set($key, $value);
            }
          }
          if (isset($this->{$this->modelClass}->validationErrors) && !empty($this->{$this->modelClass}->validationErrors)) {
            $this->set("formError", $this->{$this->modelClass}->validationErrors);
          }

          if (isset($this->viewVars["params"])) {
            unset($this->viewVars["params"]);
          }
          if (isset($this->viewVars["request"])) {
            unset($this->viewVars["request"]);
          }
          $response = $this->viewVars;

          if (!in_array($this->params["action"], $this->Auth->allowedActions) && !$this->Auth->loggedIn()) {
            $response = array("authError" => true, "message" => "Please login to access.");
          }
          $this->set(compact("response"));
          $this->set('_serialize', array("response"));
        }
      }

      public function redirect($url, $status = NULL, $exit = true) {
        if ($this->params["ext"] == "json") {

          $paging = $requests = "";

          if (isset($this->params["paging"]) && !empty($this->params["paging"])) {
            $paging = $this->params["paging"];
          }
          $this->set(compact("paging"));

          if (isset($this->request->data) && !empty($this->request->data)) {
            $requests = $this->request->data;
          }
          $this->set(compact("requests"));

          if (isset($this->{$this->modelClass}->validationErrors) && !empty($this->{$this->modelClass}->validationErrors)) {
            $this->set("formError", $this->{$this->modelClass}->validationErrors);
          }
          if (!in_array($this->params["action"], $this->Auth->allowedActions) && !$this->Auth->loggedIn()) {
            $response = array("authError" => true, "message" => "Please login to access.");
          }
          $this->set(compact("response"));
          $this->set('_serialize', array("response"));
        } else {
          parent::redirect($url, $status = NULL, $exit = true);
        }
      }

注意:如果您正在检查 Ajax 请求是否使用该请求是否 ajax,并且您想要回复您的 ajax,您必须将您的回复放在"IF"条件

if (!isset($this->params["ext"])) {
           // echo json_encode($response);
           // echo "success";
           // die; 
        }

否则您可以渲染您的视图作为响应。