Esempio n. 1
0
 protected function handle()
 {
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         try {
             $name = $posts->get('name');
             if (!$name) {
                 throw new \Exception('标签名不能为空');
             }
             $description = $posts->get('description');
             $slug = $posts->get('slug');
             if (!$slug) {
                 $slug = null;
             }
             // 判断slug是否重复
             if ($slug) {
                 $tag = TagModel::getTagBySlug($slug);
                 if ($tag) {
                     throw new \Exception('标签URL占位符已被使用');
                 }
             }
             // 创建tag
             $tag = new TagModel();
             $tag->name = $name;
             $tag->description = $description;
             $tag->slug = $slug;
             $now = time();
             $tag->createTimestamp = $now;
             $tag->updateTimestamp = $now;
             // 保存
             $tag = TagModel::createTag($tag);
             return new JsonResponse(array('status' => 1, 'data' => array('tag' => $tag->toArray())));
         } catch (\Exception $e) {
             return new JsonResponse(array('status' => 0, 'data' => $e->getMessage()));
         }
     }
     return $this->render('tag/add.html.twig');
 }