Laravel: Übergabe von Daten an default.blade.php vom Basis-controller

Ich habe einen Basis-controller mit einer Methode zurückgegeben, ein twitter-feed, um meine Ansicht.

Möchte ich diese verschieben in der Ansicht von der Seite Ansicht auf die Standard-Klinge, um Redundanz zu reduzieren, wie es erscheinen wird, die Website breit. Wie übertrage ich Daten von der Basis-controller Klinge?

Kann ich senden Sie es an meine Ansicht von der Seite controller-wie so:

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

und in der Ansicht die Ausgabe wie diese:

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

Ich will alles tun, was dieser von innerhalb default.blade.php und meine 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;
    }
}

Wie ist das möglich?

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

application/models/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;
    }
}

application/views/widgets/tweets.blade.php

@foreach ($tweets)
    test
@endforeach

application/views/layouts/default.blade.php

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

application/composers.php

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

application/controllers/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');

    }

}

application/controllers/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
        ));
    }
}

Ist mir

Undefined variable: tweets

Fehler

  • Wollen Sie Ihre tweets gerendert in Ihrem layout oder immer verschachtelt ist der Inhalt?
  • Entweder, um ehrlich zu sein. Ich bin nur Rendern Sie in der Ansicht aktuell, bis ich eine bessere/sauberere Weg, es zu tun
  • Aber Sie wollen würde, um zu finden einen Platz in Ihrem layout für Sie statt? Ich Frage deshalb, weil es mir helfen wird, schreiben die bessere Antwort für Sie 🙂
  • Das ist richtig, ja. Danke Phil 🙂
InformationsquelleAutor Fraser | 2013-04-16
Schreibe einen Kommentar