我有一个基本控制器,有一个方法可以将Twitter提要返回给我的视图.
我想在视图中将其从页面视图移动到默认刀片,以减少冗余,因为它将出现在网站范围内.如何将数据从基本控制器传递到刀片?
我可以从页面控制器发送到我的视图,如下所示:
public function get_index()
{
..................
$this->layout->nest('content', 'home.index', array(
'tweets' => $this->get_tweet()
));
}
Run Code Online (Sandbox Code Playgroud)
并在视图中,输出如下:
if ($tweets)
{
foreach ($tweets as $tweet)
{
..............
Run Code Online (Sandbox Code Playgroud)
我想在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;
}
}
Run Code Online (Sandbox Code Playgroud)
这怎么可能?
////////////////////// UPDATE /////////////////////////// //
应用程序/模型/ 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;
}
}
Run Code Online (Sandbox Code Playgroud)
应用/视图/窗口小部件/ tweets.blade.php
@foreach ($tweets)
test
@endforeach
Run Code Online (Sandbox Code Playgroud)
应用/视图/布局/ default.blade.php
....
{{ $tweets }}
....
Run Code Online (Sandbox Code Playgroud)
应用程序/ composers.php
<?php
View::composer('widgets.tweets', function($view)
{
$view->tweets = Tweets::get();
});
View::composer('layouts.default', function($view)
{
$view->nest('tweets', 'widgets.tweets');
});
Run Code Online (Sandbox Code Playgroud)
应用/控制器/ 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');
}
}
Run Code Online (Sandbox Code Playgroud)
应用/控制器/ 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
));
}
}
Run Code Online (Sandbox Code Playgroud)
给我一个
未定义的变量:推文
错误
Phi*_*rks 12
第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');
}
}
Run Code Online (Sandbox Code Playgroud)
View::share('key', 'value');
Run Code Online (Sandbox Code Playgroud)
在你看来使用(刀片语法)
{{$key}}
Run Code Online (Sandbox Code Playgroud)
或(PHP语法)
echo $key;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5822 次 |
| 最近记录: |