2010-10-15 128 views
0

我有主控制器,打印主頁。如何在視圖或其他控制器中調用控制器方法?

<? 

class Main extends Controller { 

    function Main() 
    { 
     parent::Controller(); 

     $this -> load -> helper('date'); 
    } 

    function index() 
    { 
     $this -> load -> view('header'); 
     $this -> load -> view('main'); 
     $this -> load -> view('footer'); 
    } 
} 

?> 

而且我有文章的控制器,打印最後6篇文章。

<?php 

class Articles extends Controller 
{ 
    function Articles() 
    { 
     parent::Controller(); 
    } 

    function top() 
    { 
     $this -> db -> limit(0, 6); 
     $query = $this -> db -> get('articles'); 

     $data['articles'] = $query -> result(); 

     $this -> load -> view('articles-top', $data); 
    } 

?> 

頁眉觀點是這樣的:

<html> 
    <head> 
    ... 
    </head> 
    <body> 

     <div id="last-articles"> 
      <!-- Here I want print last 6 articles --> 
     </div> 

     <div id="content"> 

我怎樣才能打印頭鑑於去年的文章?

+1

看起來像一個CI代碼,而不是Kohana的 – biakaveron 2010-10-15 10:07:23

+0

我重新標記這笨。 Kohana並沒有花費這個 - >負載 - > ...很長一段時間。 – 2010-10-18 19:32:59

回答

0

你這樣做是錯的。

你的控制器需要有一個名爲像這樣的功能...

public function action_youractionname($value='') 
{ 
    $array_of_data_for_view = array('data' => $some values); 
    echo View::factory('viewfilename', $array_of_data_for_view)->render(); 
} 

您的視圖可以使用$數據變量。

希望這會有所幫助,但我建議這個網站.. http://kerkness.ca/wiki/doku.php

它是非常寶貴的給我。

祝你好運!

0

$ data數組中的數組鍵變爲視圖中的變量。

Code Igniter User Guide非常有用。

你可以做你的看法是這樣的:

<html> 
<head> 
... 
</head> 
<body> 

    <div id="last-articles"> 
     <?php 
      foreach($articles as $article) 
      { 
       echo $article.title; //obviously these would be the real fields 
       echo $article.content; //from your article table. 
      } 
     ?> 
    </div> 

    <div id="content"> 
相關問題