【问题标题】:Laravel: Passing data to default.blade.php from base controllerLaravel:将数据从基本控制器传递给 default.blade.php
【发布时间】:2013-04-08 14:39:59
【问题描述】:

我有一个基本控制器,它有一种方法可以将 twitter 提要返回到我的视图中。

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

我可以像这样从页面控制器将它发送到我的视图:

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

在视图中,输出如下:

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

应用程序/视图/小部件/tweets.blade.php

@foreach ($tweets)
    test
@endforeach

应用程序/视图/布局/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');
});

应用程序/控制器/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
        ));
    }
}

给我一​​个

未定义变量:推文

错误

【问题讨论】:

  • 您希望您的推文在您的布局中呈现,还是始终嵌套在内容中?
  • 要么说实话。我目前只是在视图中渲染它们,直到找到更好/更清洁的方法
  • 但是你想在你的布局中为他们找到一个空间?我问是因为它会帮助我为你写出更好的答案:)
  • 没错没错。谢谢菲尔:)

标签: laravel laravel-3


【解决方案1】:

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

第 2 步 - 将推文数据传递到您的推文视图中,为此我们使用 View Composer,这样逻辑就保留在视图中(但在视图之外)。

第 3 步 - 创建您的默认布局,我们称之为layout/default.blade.php。这将接受$content$tweets。我们将用另一个 View Composer 嵌套 tweets 视图。您可以在控制器操作中嵌套$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');
    }

}

【讨论】:

  • 太棒了。非常感谢您抽出宝贵的时间。非常感谢。我最近才开始使用 Laravel,但很喜欢它!
  • 您好,再次感谢上述内容。我已经尝试实现它,但我得到了未定义的变量:来自 default.blade 的推文。有任何想法吗?我已经用我的代码更新了我上面的帖子。干杯
  • 嗨@Fraser 我完全忘记了一步,您需要包含composer.php文件,您可以在application/start.php中执行此操作
  • 谢谢菲尔。我似乎在 start.php 中看不到任何包含?
  • 我更新了我的答案,我意识到那里已经没有任何包含了。在底部添加:include path('app').'composers.php';
【解决方案2】:
View::share('key', 'value');

在你看来使用(刀片语法)

{{$key}}

或(PHP 语法)

echo $key;

【讨论】:

  • 不要总是依赖View::share()——它对于某些事情来说是一个很好的解决方案,但在许多情况下它和global variable一样糟糕
猜你喜欢
  • 2020-10-06
  • 1970-01-01
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 2019-07-16
  • 2019-01-06
相关资源
最近更新 更多