Tôi có bộ điều khiển cơ sở với phương thức trả lại nguồn cấp dữ liệu twitter cho chế độ xem của tôi.Laravel: Chuyển dữ liệu về mặc định.blade.php từ bộ điều khiển cơ sở
Tôi muốn di chuyển mục này từ chế độ xem trang sang lưỡi mặc định để giảm dự phòng vì nó sẽ xuất hiện trên toàn trang. Làm cách nào để truyền dữ liệu từ bộ điều khiển cơ sở sang lưỡi dao?
tôi có thể gửi nó cho quan điểm của tôi từ bộ điều khiển trang như vậy:
public function get_index()
{
..................
$this->layout->nest('content', 'home.index', array(
'tweets' => $this->get_tweet()
));
}
và trong giao diện, sản lượng nó như thế này:
if ($tweets)
{
foreach ($tweets as $tweet)
{
..............
tôi muốn làm tất cả điều này từ bên trong default.blade.php và Base_Contoller của tôi:
<?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;
}
}
Làm cách nào có thể?
////////////////////// UPDATE /////////////////////// //////
application/mô hình/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 }}
....
.210
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');
}
}
application/controllers/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
));
}
}
là đem lại cho tôi một
Un định nghĩa biến: tweets
lỗi
Bạn có muốn tweets của bạn render trong cách bố trí của bạn, hoặc luôn luôn lồng trong nội dung? –
Hoặc là trung thực. Tôi chỉ cần hiển thị chúng trong chế độ xem hiện tại cho đến khi tôi tìm thấy cách tốt hơn/sạch hơn để thực hiện nó – Fraser
Nhưng bạn muốn tìm một không gian trong bố cục của mình cho chúng thay thế? Tôi hỏi vì nó sẽ giúp tôi viết câu trả lời tốt hơn cho bạn :) –