在我的應用程序中,用戶可能有也可能沒有配置文件。一個例子是:如果用戶沒有個人資料顯示404
Array
(
[User] => Array
(
[id] => 7
[username] => noprofile
[password] => 3b8cdb0c849f00f3634d9b29def1dac4e9235795
[email] => [email protected]
[status] => 0
[code] => 114a10ebb6ffe364805aa70cd44bee7c
)
[Profile] => Array
(
[id] =>
[firstname] =>
[lastname] =>
[gender] =>
[dob] =>
[user_id] =>
)
)
在這裏,用戶noprofile在配置文件表中沒有匹配的內容。
如何在發生這種情況時顯示404?我試過了:
public function view ($username)
{
$this->Profile->User->recursive = 2;
$profile = $this->Profile->User->find('first', array(
'conditions' => array('User.username' => $username)
));
if (empty($profile))
{
echo 'echo1';
throw new NotFoundException('No profile');
}
else {
echo 'echo2';
}
$this->set(compact('profile'));
}
但它沒有顯示404!我正在使用CakePHP 2.0。
這是因爲它找到了用戶,因此配置文件不是空的!如何檢查配置文件爲空?
檢查$ profile的值,並檢查是否確實正在輸入if語句並執行cakeError。 – Jrod 2011-12-30 16:02:35
如何檢查,因爲我沒有IDE來檢查是否到達 – Cameron 2011-12-30 16:10:54
您不需要IDE。在if語句之前打印/回顯$ profile變量,然後在if語句中打印/回顯一個字符串,如「嘿,我在IF語句中」。 – Jrod 2011-12-30 16:17:47