瀏覽代碼

Lesson 23: DI, auto-resolution, and repositories.

Frederic G. MARAND 7 年之前
父節點
當前提交
e1fa55782b
共有 4 個文件被更改,包括 305 次插入188 次删除
  1. 11 4
      app/Http/Controllers/PostsController.php
  2. 14 0
      app/Repositories/Posts.php
  3. 1 1
      composer.json
  4. 279 183
      composer.lock

+ 11 - 4
app/Http/Controllers/PostsController.php

@@ -3,16 +3,22 @@
 namespace App\Http\Controllers;
 
 use App\Post;
+use App\Repositories\Posts;
 use Carbon\Carbon;
 use Illuminate\Http\Request;
 
 class PostsController extends Controller
 {
+    /**
+     * @var \App\Repositories\Posts
+     */
+    protected $posts;
 
-    public function __construct()
+    public function __construct(Posts $posts)
     {
         $this->middleware('auth')
         ->except(['index', 'show']);
+        $this->posts = $posts;
     }
 
     /**
@@ -56,9 +62,10 @@ class PostsController extends Controller
      */
     public function index(Request $request)
     {
-        $posts = Post::latest()
-            ->filter($request->only(['month', 'year']))
-            ->get();
+        $posts = $this->posts->all();
+//        $posts = Post::latest()
+//            ->filter($request->only(['month', 'year']))
+//            ->get();
 
         return view('posts.index', compact('posts'));
     }

+ 14 - 0
app/Repositories/Posts.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Repositories;
+
+
+use App\Post;
+
+class Posts
+{
+    public function all()
+    {
+        return Post::all();
+    }
+}

+ 1 - 1
composer.json

@@ -39,7 +39,7 @@
     "laravel/tinker": "~1.0"
   },
   "require-dev": {
-    "fgm/container-debug": "5.0.x-dev",
+    "daylerees/container-debug": "5.0.x-dev",
     "fzaninotto/faker": "~1.4",
     "mockery/mockery": "0.9.*",
     "phpunit/phpunit": "~5.7"

文件差異過大導致無法顯示
+ 279 - 183
composer.lock


部分文件因文件數量過多而無法顯示