2012-07-29 84 views
0

我嘗試在CakePHP 2.1中開發一個簡單的webservices。所以,我首先創建一個數據庫表:CakePHP和RESTful webservices

CREATE TABLE IF NOT EXISTS `people` (
    `id` char(36) COLLATE utf8_bin NOT NULL, 
    `first_name` varchar(100) COLLATE utf8_bin DEFAULT NULL, 
    `last_name` varchar(100) COLLATE utf8_bin NOT NULL, 
    `home_address` varchar(100) COLLATE utf8_bin DEFAULT NULL, 
    `job_address` varchar(100) COLLATE utf8_bin DEFAULT NULL, 
    `phone` varchar(20) COLLATE utf8_bin DEFAULT NULL, 
    `fax` varchar(20) COLLATE utf8_bin DEFAULT NULL, 
    `mail` varchar(100) COLLATE utf8_bin DEFAULT NULL, 
    `birth_date` int(11) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin 

比我創建了一個簡單的人模型:

<?php 
class Person extends AppModel { 
    public $name = 'Person'; 
} 

和各自的控制器:

<?php 
class PeopleController extends AppController { 
    public $components = array('RequestHandler'); 

    public function index() {  
     $people = $this->Person->find('all'); 
     $this->set(array(
      'people' => $people, 
      '_serialize' => array('person') 
     )); 
    } 
} 

終於,在路線。 php,我已經繪製出正確的roote:

<?php 
Router::mapResources('people'); 
Router::parseExtensions('json'); 
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); 
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); 
CakePlugin::routes(); 
require CAKE . 'Config' . DS . 'routes.php'; 

但是當我試圖達到ht_tp://localhost/cakephp/people.json我得到:

<pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr501541d86308c-trace').style.display = (document.getElementById('cakeErr501541d86308c-trace').style.display == 'none' ? '' : 'none');"><b>Notice</b> (8)</a>: Undefined index: person [<b>CORE\Cake\View\JsonView.php</b>, line <b>89</b>]<div id="cakeErr501541d86308c-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr501541d86308c-code').style.display = (document.getElementById('cakeErr501541d86308c-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr501541d86308c-context').style.display = (document.getElementById('cakeErr501541d86308c-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr501541d86308c-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;autoRender&nbsp;=&nbsp;false;</span></code> 
<code><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;View&nbsp;=&nbsp;$View;</span></code> 
<span class="code-highlight"><code><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;response-&gt;body($View-&gt;render($view,&nbsp;$layout));</span></code></span></pre><pre id="cakeErr501541d86308c-context" class="cake-context" style="display: none;">$view = null 
$layout = null 
$serialize = array(
    (int) 0 =&gt; &#039;person&#039; 
) 
$data = array() 
$key = &#039;person&#039;</pre><pre class="stack-trace">JsonView::render() - CORE\Cake\View\JsonView.php, line 89 
Controller::render() - CORE\Cake\Controller\Controller.php, line 957 
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 193 
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 161 
[main] - APP\webroot\index.php, line 92</pre></div></pre>{"person":null} 

哪裏錯誤?

回答

1

正如你所看到的,你得到的迴應是HTML。它說什麼(當你在瀏覽器中打開時)是:

Notice (8): Undefined index: person [CORE\Cake\View\JsonView.php, line 89] 
Code Context 
JsonView::render() - CORE\Cake\View\JsonView.php, line 89 
Controller::render() - CORE\Cake\Controller\Controller.php, line 957 
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 193 
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 161 
[main] - APP\webroot\index.php, line 92 
{"person":null} 

你有沒有在數據庫中的任何記錄? 那麼這種方法的觀點呢?它應該是這樣的:

// app/View/People/json/index.ctp 
<?php 
echo json_encode(compact('people')); 

注意,index.ctp文件位於JSON子文件夾中。 請在JSON and XML views上查看書籍,如果還有其他問題,請發表評論。

1

錯誤清楚地表明你已經設置$連載,不是$ _serialize viewVar

使用「_serialize」就像你在你自己的代碼示例有。

還要確保你想要序列化的var匹配存在的viewVar。

你序列化$ person,而你設置$ people