매개 변수 $id
받아 들여야 액션 : 당신이해야 할 일은
이
public function actionView($id) {
$model = $this->loadModel($id);
, 동일한 컨트롤러에 loadModel 기능을 수정하는 것입니다 : 다음
/**
* @param integer or string the ID or slug of the model to be loaded
*/
public function loadModel($id) {
if(is_numeric($id)) {
$model = Page::model()->findByPk($id);
} else {
$model = Page::model()->find('slug=:slug', array(':slug' => $id));
}
if($model === null)
throw new CHttpException(404, 'The requested page does not exist.');
if($model->hasAttribute('isDeleted') && $model->isDeleted)
throw new CHttpException(404, 'The requested page has been deleted for reasons of moderation.');
// Not published, do not display
if($model->hasAttribute('isPublished') && !$model->isPublished)
throw new CHttpException(404, 'The requested page is not published.');
return $model;
}
, 당신은 수정해야합니다 urlManager 규칙은 ID 대신 문자열을 허용합니다.
아래 기본 규칙에서 :\d+
을 제거하십시오.
'<controller:\w+>/<id:\d+>' => '<controller>/view',
그것은 다음과 같이해야합니다 :
'<controller:\w+>/<id>' => '<controller>/view',
이 경로를가는 경우, 반드시 슬러그는 데이터베이스에서 고유하게, 당신은 또한 유효성 검사 규칙을 적용해야한다,주의해야 할 한 가지 더 모델에서 :