2016-12-09 8 views
0

私はProductControllerというコントローラを作成しました。そのコントローラーでは、ページネーションを追加しましたが、他のコントローラーのそれぞれで同じコードを繰り返します。いいえ。主に、Requestオブジェクトと$ this-> validate関数のために、以下のコードを独自の静的ヘルパー関数に移動する方法を理解できません。私は古い手続きプログラマーであり、私はまだLaravelの周りを包み込んでいます。任意の提案をいただければ幸いです。

class ProductController extends Controller 
{ 
    /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index(Request $request) 
    { 

     // Pagination. Grab sort_by, direction and page size. If empty assign defaults 
     $sort_by = (empty($request->input('sort_by'))) ? 'id' : $request->input('sort_by'); 
     $direction = (empty($request->input('direction'))) ? 'asc' : $request->input('direction'); 
     $page_size = (empty($request->input('page_size'))) ? '10' : $request->input('page_size'); 

     // Validate pagination input from URL 
     $this->validate($request, [ 
      'sort_by' => 'alpha_dash', 
      'direction' => 'alpha_dash', 
      'page_size' => 'integer' 
     ]); 
     // Keeps page size less than a 1000 records being pulled 
     if ($page_size > 1000) $page_size = 1000; 

     $records = Product::with('manufacturer')->with('source')->where('active', 1)->orderBy($sort_by, $direction)->paginate($page_size); 
     $record_count = Product::where('active', 1)->count(); 

     return view('pages.products.index', compact('records','sort_by','direction','page_size','record_count')); 
    } 
+1

すべての楽しさで繰り返されるコードの一部であり、どのようなction? – Jerodev

+0

すべての*拡張コントローラ*にある必要があるコードの部分は、すべての拡張コントローラで使用できるように 'Controller'クラス自体に入ります。 – WEBjuju

+0

それは私に共通のコードを作る必要があると思わない...?すべての人に共通のコードを教えてください。 –

答えて

2

ヘルパー:

class Helper { 
    public static function handlePagination($request, $validate, $rules = []) { 
     // Pagination. Grab sort_by, direction and page size. If empty assign defaults 
     $sort_by = (empty($request->input('sort_by'))) ? 'id' : $request->input('sort_by'); 
     $direction = (empty($request->input('direction'))) ? 'asc' : $request->input('direction'); 
     $page_size = (empty($request->input('page_size'))) ? '10' : $request->input('page_size'); 

     // Validate pagination input from URL 
     $validate($request, [ 
      'sort_by' => 'alpha_dash', 
      'direction' => 'alpha_dash', 
      'page_size' => 'integer' 
     ]); // @TODO: append $rules to ruleset 

     // Keeps page size less than a 1000 records being pulled 
     if ($page_size > 1000) { 
      $page_size = 1000; 
     } 

     return compact('sort_by', 'direction', 'page_size'); 
    } 
} 

コントローラ:

class ProductController extends Controller 
{ 
    /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index(Request $request) 
    { 
     list($sort_by, $direction, $page_size) = Helper.handlePagination($request, $this->validate); 

     $records = Product::with('manufacturer') 
      ->with('source') 
      ->where('active', 1) 
      ->orderBy($sort_by, $direction) 
      ->paginate($page_size); 
     $record_count = Product::where('active', 1)->count(); 

     return view('pages.products.index', compact('records','sort_by','direction','page_size','record_count')); 
    } 
} 
+0

少し変更してコードを使用しましたが、それは完璧に機能しました。ありがとう! –

4

複製されたコードを含むベースコントローラを作成し、そのベースコントローラを他のものに拡張することができます。

namespace App\Http\Controllers; 

class myController extends Controller { 

    public function paginate(Request $request){ 

     // Pagination. Grab sort_by, direction and page size. If empty assign defaults 
     $sort_by = (empty($request->input('sort_by'))) ? 'id' : $request->input('sort_by'); 
     $direction = (empty($request->input('direction'))) ? 'asc' : $request->input('direction'); 
     $page_size = (empty($request->input('page_size'))) ? '10' : $request->input('page_size'); 

     // Validate pagination input from URL 
     $this->validate($request, [ 
      'sort_by' => 'alpha_dash', 
      'direction' => 'alpha_dash', 
      'page_size' => 'integer' 
     ]); 
     // Keeps page size less than a 1000 records being pulled 
     if ($page_size > 1000) $page_size = 1000; 
      $records = Product::with('manufacturer')->with('source')->where('active', 1)->orderBy($sort_by, $direction)->paginate($page_size); 
      $record_count = Product::where('active', 1)->count(); 
      return view('pages.products.index', compact('records','sort_by','direction','page_size','record_count')); 
     } 
    } 
} 

あなたのコントローラー

namespace App\Http\Controllers; 

class ProductController extends myController { 
    public function index(Request $request){ 
     $this->paginate(); 
    } 
} 
関連する問題