Laravel: passing data to default.blade.php from the base controller

I have a base controller with a Twitter feed return method for my view.

I want to move this in the view from page view to default click to reduce redundancy as it will appear on the site. How to transfer data from the base controller to the blade server?

I can send it in my opinion from the page manager as follows:

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

and in the view print it like this:

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

I want to do all this from within default.blade.php and my 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;
    }
}

How is this possible?

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

    }

}

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

Undefined :

+5
2

1 - , widgets/tweets.blade.php, $tweets. , . , .

2 - , View Composer, ( ).

3 - , layout/default.blade.php. $content $tweets. View Composer. $content .

4 - $layout Base_Controller.

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');
    }

}
+12
View::share('key', 'value');

( Blade)

{{$key}}

( PHP)

echo $key;
+6

All Articles