2013-04-16 19 views
5

twitterフィードを自分のビューに返すメソッドを持つベースコントローラーがあります。Laravel:ベースコントローラーからdefault.blade.phpにデータを渡す

これをページビューからデフォルトブレードに移動して、サイト全体に表示される冗長性を減らしたいとします。ベースコントローラからブレードにデータを渡すにはどうすればよいですか?

私はそうのようなページコントローラから私の見解にそれを送ることができます:私は内からこのすべてをやりたい

if ($tweets) 
{ 
    foreach ($tweets as $tweet) 
    { 
     .............. 

:、

public function get_index() 
{ 
    .................. 
    $this->layout->nest('content', 'home.index', array(
     'tweets' => $this->get_tweet() 
    )); 
} 

とビューのこのような出力をdefault.blade.phpと私のBase_Contoller:

<?php 
class Base_Controller extends Controller { 
    /** 
    * Catch-all method for requests that can't be matched. 
    * 
    * @param string $method 
    * @param array  $parameters 
    * @return Response 
    */ 
    public function __call($method, $parameters) 
    { 
     return Response::error('404'); 
    } 

    public function get_tweet() 
    { 
     ........... 
     return $tweets; 
    } 
} 

どのようにこれは可能ですか?

////////////////////// UPDATE /////////////////////////// //////

アプリケーション/モデル/ tweets.php

<?php 
class Tweets { 
    public static function get($count = 3) 
    { 
     Autoloader::map(array(
     'tmhOAuth'  => path('app'). 
       'libraries/tmhOAuth-master/tmhOAuth.php', 
      'tmhUtilities' => path('app'). 
       'libraries/tmhOAuth-master/tmhUtilities.php' 
     )); 
     $tmhOAuth = new tmhOAuth(array(
      'consumer_key'  => 'xxx', 
      'consumer_secret'  => 'xxx', 
      'user_token'   => 'xxxxx', 
      'user_secret'   => 'xxxxx', 
      'curl_ssl_verifypeer' => false 
     )); 
     $code = $tmhOAuth->request('GET', 
     $tmhOAuth->url('1.1/statuses/user_timeline'), array(
      'screen_name' => 'xxx', 
      'count' => $count 
     )); 
     $response = $tmhOAuth->response['response']; 
     $tweets = json_decode($response, true); 
     return $tweets; 
    } 
} 

アプリケーション/ビュー/ウィジェット/ tweets.blade.php

@foreach ($tweets) 
    test 
@endforeach 

アプリケーション/ビュー/レイアウト/デフォルト。 blade.php

.... 
{{ $tweets }} 
.... 

アプリケーション/ composers.php

<?php 
View::composer('widgets.tweets', function($view) 
{ 
    $view->tweets = Tweets::get(); 
}); 
View::composer('layouts.default', function($view) 
{ 
    $view->nest('tweets', 'widgets.tweets'); 
}); 

アプリケーション/コントローラ/ Base.phpという

<?php 
class Base_Controller extends Controller { 

    /** 
    * Catch-all method for requests that can't be matched. 
    * 
    * @param string $method 
    * @param array  $parameters 
    * @return Response 
    */ 
    public $layout = 'layouts.default'; 

    public function __call($method, $parameters) 
    { 
     return Response::error('404'); 

    } 

} 

アプリケーション/コントローラ/ home.php

<?php 
class Home_Controller extends Base_Controller { 

    public $layout = 'layouts.default'; 

    public $restful = true; 

    public function get_index() 
    { 
     Asset::add('modernizr', 'js/thirdparty/modernizr.js'); 
     Asset::add('jquery', 
      'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'); 
     Asset::add('scripts', 'js/scripts.js'); 

     $this->layout->title = 'title'; 
     $this->layout->nest('content', 'home.index', array(
      //'data' => $some_data 
     )); 
    } 
} 

は私に

を与えています

Un定義された変数:ツイート

エラーが

+0

でそれらを含める必要がありますが、あなたのつぶやきがあなたのレイアウトでレンダリングしたい、または常にコンテンツ内にネストされていますか? –

+0

どちらか正直です。私はそれを行うより良い/クリーンな方法を見つけるまで現在のビューでそれらをレンダリングしています – Fraser

+0

しかし、それらの代わりにあなたのレイアウトのスペースを探したいでしょうか?私はそれがあなたのためにより良い答えを書くのに役立つので、私は尋ねています:) –

答えて

12

ステップ1の使用を表示して - くださいあなたのつぶやきだけを見て、それをwidgets/tweets.blade.phpと呼んでみましょう。あなたの$tweetsデータを受け入れます。これにより、少しでもパフォーマンスを向上させたい場合は、ツイートの表示を将来的にキャッシュするのが非常に簡単になります。また、あなたのためにツイートデータを生成するモデルが必要です。

ステップ2 - ツイートのデータをtweetビューに渡し、View Composerを使用して、ロジックをビューと一緒に(しかし外側に)保ちます。

ステップ3 - デフォルトのレイアウトを作成します。これをlayout/default.blade.phpとします。これは$content$tweetsを受け入れます。つぶやきの表示を別のView Composerと入れ子にします。コントローラーアクションには$contentをネストすることができます。

ステップ4 - Base_Controller$layoutを設定します。

ステップ5 - 利益!

注 - これらはあなたの最初のビューを作曲しているなら、あなたはapplication/start.php


// application/models/tweets.php 
class Tweets { 
    public static function get($count = 5) 
    { 
     // get your tweets and return them 
    } 
} 

// application/views/widgets/tweets.blade.php 
@foreach ($tweets) 
    {{-- do something with your tweets --}} 
@endforeach 

// application/views/layouts/default.blade.php 
<section class="main">{{ isset($content) ? $content : '' }}</section> 
<aside class="widget widget-tweets">{{ $tweets }}</aside> 

// application/composers.php 
View::composer('widgets.tweets', function($view) 
{ 
    $view->tweets = Tweets::get(); 
}); 
View::composer('layouts.default', function($view) 
{ 
    $view->nest('tweets', 'widgets.tweets'); 
}); 

// application/start.php (at the bottom) 
include path('app').'composers.php'; 

// application/controllers/base.php 
class Base_Controller extends Controller { 
    public $layout = 'layouts.default'; 
} 

// application/controllers/home.php 
class Home_Controller extends Base_Controller { 

    public $restful = true; 

    public function get_index() 
    { 
     $this->layout->nest('content', 'home.welcome'); 
    } 

} 
+0

素晴らしいです。これで時間をとってくれてありがとうございます。非常に感謝しています。私は最近Laravelを始めたばかりですが、それを愛しています! – Fraser

+0

こんにちは、上記のおかげで再び。私はそれを実装しようとしましたが、未定義の変数を取得しています:default.bladeからのつぶやき。何か案は?上記の投稿を自分のコードで更新しました。 Cheers – Fraser

+0

こんにちは@Fraser私は完全にステップを忘れました、あなたは 'application/start.php'でこれを行うことができますcomposers.phpファイルを含める必要があります –

6
View::share('key', 'value'); 

あなたは(ブレイド構文)

{{$key}} 

か(PHP構文)

echo $key; 
+1

'View :: share()'にいつも戻ってくるわけではありません - いくつかの点では素晴らしい解決策ですが、多くの場合、[グローバル変数](http://c2.com/ cgi/wiki?GlobalVariablesAreBad) –

+0

@PhillSparks、同意します。 – Usman

関連する問題