2013-04-16 27 views
5

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

+0

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? –

+0

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

+0

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 :) –

Trả lời

12

Bước 1 - Tạo một chỉ xem cho các tweet của bạn, hãy gọi nó là widgets/tweets.blade.php, sẽ chấp nhận dữ liệu $tweets của bạn. Điều này làm cho nó rất dễ dàng để bộ nhớ cache xem tweets trong tương lai nếu bạn muốn có thêm một chút hiệu suất. Chúng tôi cũng muốn một mô hình sẽ tạo ra dữ liệu tweet cho bạn.

Bước 2 - Chuyển dữ liệu tweet vào chế độ xem tweet của bạn, hãy sử dụng View Composer cho điều này để logic được giữ với (nhưng bên ngoài) chế độ xem.

Bước 3 - Tạo bố cục mặc định của bạn, hãy gọi số này layout/default.blade.php. Điều này sẽ chấp nhận $content$tweets. Chúng tôi sẽ lồng khung nhìn tweets bằng một số khác View Composer. Bạn có thể lồng các $content trong hành động điều khiển của bạn.

Bước 4 - Đặt số $layout trên số Base_Controller của bạn.

Bước 5 - Lợi nhuận!

Lưu ý - Nếu đây là những quan điểm nhà soạn nhạc đầu tiên của bạn sau đó bạn sẽ cần phải bao gồm chúng trong 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'); 
    } 

} 
+0

Tuyệt vời. Cảm ơn bạn rất nhiều vì đã dành thời gian cho việc này. Nó rất được đánh giá cao. Tôi vừa mới bắt đầu ở Laravel nhưng yêu nó! – Fraser

+0

Xin chào, Cảm ơn bạn lần nữa vì đã nói ở trên. Tôi đã thử thực hiện nó nhưng đang nhận được biến không xác định: tweets từ default.blade. Ý tưởng nào? Tôi đã cập nhật bài đăng của mình ở trên bằng mã của tôi. Chúc mừng – Fraser

+0

Hi @Fraser Tôi hoàn toàn quên một bước, bạn cần phải bao gồm các tập tin composers.php, bạn có thể làm điều này trong 'application/start.php' –

6
View::share('key', 'value'); 

trong bạn xem sử dụng (Blade cú pháp)

{{$key}} 

hoặc (PHP cú pháp)

echo $key; 
+1

Không phải lúc nào cũng quay trở lại với 'View :: share()' - đó là một giải pháp tuyệt vời cho một số thứ nhưng trong nhiều trường hợp nó xấu như [global variable] (http://c2.com/ cgi/wiki? GlobalVariablesAreBad) –

+0

@PhillSparks, đồng ý. – Usman