Browse Source

修复文章分类删除后造成的bug

Moell 9 years ago
parent
commit
c129a618d8

+ 2 - 2
app/Http/Controllers/CategoryController.php

@@ -19,14 +19,14 @@ class CategoryController extends Controller
     public function index($id)
     {
         try {
-            $category = $this->category->find($id);
+            $category = $this->category->baseFind($id);
             $articles = $category->article()
                 ->orderBy('sort','desc')
                 ->orderBy('id', 'desc')
                 ->paginate(15);
             return view('default.category_article', compact('articles', 'category'));
         } catch (\Exception $e) {
-            die($e->getMessage());
+            return redirect('/');
         }
     }
 }

+ 14 - 0
app/Presenters/CategoryPresenter.php

@@ -71,4 +71,18 @@ class CategoryPresenter extends FractalPresenter
         }
         return $name;
     }
+
+    /**
+     * @param $categoryId
+     * @return string
+     */
+    public function getIdLink($categoryId)
+    {
+        $name = $this->getIdName($categoryId);
+        if ($name) {
+            return "<i class=\"glyphicon glyphicon-th-list\"></i>
+                <a href='".route('category', ['id' => $categoryId])."'' target=\"_blank\">".$name."</a>";
+        }
+        return '';
+    }
 }

+ 20 - 0
app/Repositories/CategoryRepositoryEloquent.php

@@ -91,6 +91,26 @@ class CategoryRepositoryEloquent extends BaseRepository implements CategoryRepos
         return true;
     }
 
+    /**
+     * @param $id
+     * @param array $columns
+     * @return mixed
+     */
+    public function find($id, $columns = ['*'])
+    {
+        $this->applyCriteria();
+        $this->applyScope();
+        $model = $this->model->find($id, $columns);
+        $this->resetModel();
+
+        return $this->parserResult($model);
+    }
+
+    public function baseFind($id, $columns = ['*'])
+    {
+        return parent::find($id, $columns);
+    }
+
     /**
      * Boot up the repository, pushing criteria
      */

+ 1 - 3
resources/views/backend/article/index.blade.php

@@ -58,9 +58,7 @@
                                     <td>{{ $article->read_count }}</td>
                                     <td>{{ $article->comment_count }}</td>
                                     <td>
-                                        @if($article->cate_id > 0)
-                                            {{ $category[$article->cate_id] }}
-                                        @endif
+                                        {{ $category[$article->cate_id] or '分类已删除' }}
                                     </td>
                                     <td>{{ $article->created_at }}</td>
                                     <td>

+ 1 - 9
resources/views/default/article.blade.php

@@ -1,6 +1,5 @@
 @if($articles)
     <ol class="article-list">
-        {{--@inject('userPresenter', 'App\Presenters\UserPresenter')--}}
         @inject('catePresenter', 'App\Presenters\CategoryPresenter')
         @foreach ($articles as $article)
             <li class="article-list-item">
@@ -17,15 +16,8 @@
                         <i class="glyphicon glyphicon-calendar"></i>{{ date('Y-m-d', strtotime($article->created_at)) }}
                     </span>
                             &nbsp;
-                    {{--<span>
-                        <i class="glyphicon glyphicon-user"></i> {{ $userPresenter->getUserName($article->user_id) }}
-                    </span>
-                            &nbsp;--}}
                     <span>
-                        <i class="glyphicon glyphicon-th-list"></i>
-                        <a href="{{ route('category', ['id' => $article->cate_id]) }}" target="_blank">
-                            {{ $catePresenter->getIdName($article->cate_id) }}
-                        </a>
+                        {!! $catePresenter->getIdLink($article->cate_id) !!}
                     </span>
                     <span style="float:right">
                         <i class="glyphicon glyphicon-eye-open"></i> {{ $article->read_count }} views

+ 10 - 6
resources/views/default/show_article.blade.php

@@ -45,12 +45,16 @@
             <div id="share" class="social-share"></div>
         </div>
         <div class="panel-body">
-            <p>分类:
-                <i class="glyphicon glyphicon-th-list"></i>
-                <a href="{{ route('category', ['id' => $category->id]) }}" target="_blank">
-                    {{ $category->name }}
-                </a>
-            </p>
+            @if($category)
+                <p>分类:
+                    <i class="glyphicon glyphicon-th-list"></i>
+
+                    <a href="{{ route('category', ['id' => $category->id]) }}" target="_blank">
+                        {{ $category->name }}
+                    </a>
+
+                </p>
+            @endif
             @if ($tags)
                 <p>标签:
                     <i class="glyphicon glyphicon-tags"></i>&nbsp;