我有一个基本 Controller ,有一个方法可以将 Twitter 提要返回到我的 View 。

我想将 View 中的它从页面 View 移动到默认 Blade 以减少冗余,因为它会出现在站点范围内。如何将数据从基本 Controller 传递到 Blade ?

我可以将它从页面 Controller 发送到我的 View ,如下所示:

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

在 View 中,输出如下:
if ($tweets) 
{ 
    foreach ($tweets as $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; 
    } 
} 

这怎么可能?

//////////////////////更新/////////////////////////////

应用程序/模型/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; 
    } 
} 

应用程序/ View /小部件/tweets.blade.php
@foreach ($tweets) 
    test 
@endforeach 

应用程序/ View /布局/default.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'); 
}); 

应用程序/ Controller /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'); 
 
    } 
 
} 

应用程序/ Controller /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 variable: tweets



错误

请您参考如下方法:

第 1 步 - 为您的推文创建 View ,我们称之为 widgets/tweets.blade.php ,这将接受您的 $tweets数据。如果您想要更高的性能,这使得将来缓存推文 View 变得非常容易。我们还需要一个模型来为您生成推文数据。

第 2 步 - 将推文数据传递到您的推文 View 中,让我们使用 View Composer为此,逻辑与(但在外部) View 保持一致。

第 3 步 - 创建您的默认布局,我们称之为 layout/default.blade.php .这将接受 $content$tweets .我们将使用另一个 View Composer 嵌套推文 View .您可以嵌套 $content在您的 Controller 操作中。

第 4 步 - 设置 $layout在您的 Base_Controller .

第 5 步 - 利润!

注意 - 如果这些是您的第一个 View Composer ,那么您需要将它们包含在 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'); 
    } 
 
} 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!