我有一個控制器,根據?id=
有一個來自數據庫的顯示數據,它工作正常。但是,如果你不給任何值ID獲取錯誤
error 400 Your request is invalid.
我的代碼:
public function actionIndex($id)
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->pageTitle = 'Page';
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => $id),
//if $id is not defined then error
)
);
}
$ModelPages = Pages::model()->findAll($criteria);
$this->render('index',
array(
'Model' => $ModelPages,
)
);
}
我以這樣的方式嘗試了這一點,但它並沒有幫助。
public function actionIndex($id)
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->pageTitle = 'Page';
if(empty($id)){
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => 'index'),
)
);
}
else {
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => $id),
)
);
}
$ModelPages = Pages::model()->findAll($criteria);
$this->render('index',
array(
'Model' => $ModelPages,
)
);
}
當我根據網站顯示內容時,我的解決方案是否正確(安全)?
我剛剛編輯答案 – Goodnickoff 2014-12-03 15:43:47