Laravel:将数据从基本控制器传递到 default.blade.php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16032861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: Passing data to default.blade.php from base controller
提问by Fraser
I have a base controller with a method to return a twitter feed to my view.
我有一个基本控制器,有一个方法可以将 Twitter 提要返回到我的视图。
I want to move this in the view from the page view to the default blade to reduce redundancy as it will be appearing site wide. How do I pass data from the base controller to blade?
我想在视图中将它从页面视图移动到默认刀片以减少冗余,因为它会出现在站点范围内。如何将数据从基本控制器传递到刀片?
I can send it to my view from the page controller like so:
我可以将它从页面控制器发送到我的视图,如下所示:
public function get_index()
{
..................
$this->layout->nest('content', 'home.index', array(
'tweets' => $this->get_tweet()
));
}
and in the view, output it like this:
在视图中,输出如下:
if ($tweets)
{
foreach ($tweets as $tweet)
{
..............
I want to do all this from within default.blade.php and my Base_Contoller:
我想从 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;
}
}
How is this possible?
这怎么可能?
//////////////////////UPDATE/////////////////////////////
//////////////////////更新/////////////////////////// //
application/models/tweets.php
应用程序/模型/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
应用程序/视图/小部件/tweets.blade.php
@foreach ($tweets)
test
@endforeach
application/views/layouts/default.blade.php
应用程序/视图/布局/default.blade.php
....
{{ $tweets }}
....
application/composers.php
应用程序/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
应用程序/控制器/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
应用程序/控制器/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
));
}
}
Is giving me an
正在给我一个
Undefined variable: tweets
未定义变量:推文
error
错误
回答by Phill Sparks
Step 1 - Make a view just for your tweets, let's call it widgets/tweets.blade.php
, that will accept your $tweets
data. This makes it very easy to cache the tweets view in the future if you want a little more performance. We also want a model that will generate the tweet data for you.
第 1 步 - 为您的推文创建一个视图,我们称之为widgets/tweets.blade.php
,它将接受您的$tweets
数据。如果您想要更高的性能,这使得将来缓存推文视图变得非常容易。我们还需要一个模型来为您生成推文数据。
Step 2 - Pass the tweet data into your tweets view, let's use a View Composerfor this so the logic is kept with (but outside) the view.
第 2 步 - 将推文数据传递到您的推文视图中,让我们为此使用View Composer,以便将逻辑保留在(但在视图之外)视图。
Step 3 - Create your default layout, let's call this layout/default.blade.php
. This will accept $content
and $tweets
. We'll nest the tweets view with another View Composer. You can nest the $content
in your controller actions.
第 3 步 - 创建您的默认布局,我们称之为layout/default.blade.php
. 这将接受$content
和$tweets
。我们将使用另一个View Composer嵌套推文视图。您可以将 嵌套$content
在您的控制器操作中。
Step 4 - Set the $layout
on your Base_Controller
.
第 4 步 -$layout
在您的Base_Controller
.
Step 5 - Profit!
第 5 步 - 利润!
Note - If these are your first view composers then you'll need to include them in application/start.php
注意 - 如果这些是您的第一个视图作曲家,那么您需要将它们包含在 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');
}
}
回答by Muhammad Usman
View::share('key', 'value');
in you view use (Blade syntax)
在您的视图中使用(刀片语法)
{{$key}}
or (PHP syntax)
或(PHP 语法)
echo $key;