2016-05-23 7 views
6

内のすべてのページ内のデータを示して、私はは、コアを拡張し、ガイドラインに従いLaravel

1を持っている)

class ViewComposerServiceProvider extends ServiceProvider 
{ 
    /** 
    * Bootstrap the application services. 
    * 
    * @return void 
    */ 
    public function boot() 
    { 
     view()->composer('template', function ($view) { 
      $view->with('series_list', Series::all()); 
     }); 
    } 

    /** 
    * Register the application services. 
    * 
    * @return void 
    */ 
    public function register() 
    { 
     // 
    } 
} 

2サービスプロバイダを作成します)

App\Providers\ViewComposerServiceProvider::class, 

3プロバイダを登録)テンプレートに変数をエコーする

{!! var_dump($series_list) !!} 

問題がある:

Route::get('/', '[email protected]'); 
Route::get('product/view/{id}', '[email protected]'); 
Route::get('product/detail/{id}', '[email protected]'); 
Route::get('/page/contact', '[email protected]'); 

PageControllerでとにHomeControllerが$ series_listを表示することができますが、ProductControllerは、エラーが返されます:ここで

Undefined variable: series_list in the template 

さProductController:

class ProductController extends Controller 
{ 
    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     //$this->middleware('auth'); 
    } 

    /** 
    * Show the application dashboard. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function view($id = 1) 
    { 
     return view('product/list'); 
    } 

    public function detail($id = 1) 
    { 
     return view('product/detail'); 
    } 
} 

どうもありがとう助けるために。

+0

は家あなたと製品/リストビューできますか? –

答えて

2

あなたの共有変数は 'template'というビューのみであり、ProductControllerは 'product/list'と 'product/detail'のビューを使用します。あなたはすべてのビューとデータを共有したい場合は、行うことができます:

view()->composer('*', function ($view) { 
    $view->with('series_list', Series::all()); 
}); 

、あるいはを:

view()->share('series_list, Series:all()); 

それ以外の場合は、最初のパラメータに配列を作成し、そこにすべてのビューが含ま:

view()->composer(['template', 'product.list'], function ($view) { 
    $view->with('series_list', Series::all()); 
}); 
+0

お手伝いいただきありがとうございます。奇妙な部分はproduct/listであり、商品/詳細はテンプレートを延長しています(連絡先と自宅と同じですが)連絡先と自宅のみが$ series_list変数を取得できます – user782104

+3

純粋な悪:ビューが20回ある場合、20のSQLクエリが実行されます。 'Series :: all()'を20回コールするのではなく、変数として渡すように修正しました。コード:http://pastebin.com/xLVCPVy –

+0

@GiedriusKiršysの好奇心から、私たちがこれを必要としなくてもあなたのソリューションは結果を取得しませんか? – Abhishek

0

メインコントローラを作成する MainControllerでView :: shareを使用する:: __constructorメソッドを使用すると、データのみ子コントローラをオーバーロードして共有できます

5

もう一つの方法は、少し短い

class AppServiceProvider extends ServiceProvider 
{ 
    // other methods 

    public function boot() 
    { 
     $series = Series::all(); 
     view()->share('series_list', $series); 
    } 

    // other methods 

} 
0

理由だけではなく、@Inject使用しませんか? 統計やその他の隔離されたもののように、グローバルにアクセスできるグローバルサービスを作るのは悪い考えではありません。

@inject('stats','App\Services\Stats') 

{{ $stats->series() }} 

あなただけが世界的に注入/提供するために必要なものを処理するために、グローバルなサービスクラスが必要:

namespace App\Services; 

use App\Series; 

class Stats { 

    public function series() 
    { 
     return Series::all(); 
    } 
} 
関連する問題