public function checkslugAction() { $this->view->disable(); $slug = $this->dispatcher->getParam('slug'); $topicId = intval($this->dispatcher->getParam('topicId')); if (Topic::checkSlugExists($slug, $topicId)) { $this->response->setStatusCode('409', 'Slug has been taken'); } else { $this->response->setStatusCode('200', 'ok'); } }
/** * 检测 slug 是否存在 * * @param string $slug slug 字符串 * @param int $topicId 专题 ID,用于修改的时候的重复性判断。 * @return bool */ public static function checkSlugExists($slug, $topicId = 0) { $topic = Topic::findFirst("slug='{$slug}'"); return $topic != null && $topic->id != $topicId; }