2017-07-17 37 views
0

我想創建主要網站導航,側欄導航和頁腳導航脂肪自由的環境。我剛開始使用MVC類型的框架。路由獨立的控制器和模型中不含脂肪框架

我的問題,因爲我的導航將是幾乎每一個網站的頁面上,我在想創造單獨的控制器和模型來處理這一切的工作人員,但不知道怎麼會不進行路由工作?

而且,我不知道如何處理模型中的加入,我也根本找不到關於這個網上的任何信息。

這是我目前的類別控制器

class Categories extends DB\SQL\Mapper 
{ 
    public function __construct(DB\SQL $db) 
    { 
     parent::__construct($db, 'categories'); 
    } 

    public function all() 
    { 
     $this->load(); 
     return $this->query; 
    } 

    public function getByID($id) 
    { 
     $this->load(array('id=?', $id)); 
     return $this->query; 
    } 

    public function getBySlug($category_slug) 
    { 
     $this->load(array('category_slug=?', $category_slug)); 
     return $this->query; 
    } 

    public function add() 
    { 
     $this->copyfrom('POST'); 
     $this->save(); 
    } 

    public function edit($id) 
    { 
     $this->load(array('id=?', $id)); 
     $this->copyfrom('POST'); 
     $this->update(); 
    } 

    public function delete($id) 
    { 
     $this->load(array('id=?', $id)); 
     $this->erase(); 
    } 
} 

任何意見或指針將幫助我走了很長的路要走。

在此先感謝

+0

這看起來像一個模型。不是控制器。 – lubangf

+0

這是一個模型,我的錯誤 – AlexB

回答

0

我不知道是否理解你的問題是什麼,但如果你想使可用於在控制器的所有方法的類別,你可以使用beforeRoute()方法:

class TestController extends MainController { 

    // runs before routing 
    // if another controller extends TestController and also has a 
    // beforeRoute, this will be overriden 
    function beforeRoute() { 

     // Load Categories 
     $categories = new Categories($this->db); 
     // Assiging ->all() to variable makes the method return an array of all results 
     $categoriesArray = $categories->all(); 
     // Set an array in the hive for template use 
     $this->f3->set('categories', $categoriesArray); 
     // Clear the instance 
     $categories->reset(); 

    } 

    function renderPage() { 

     // the 'categories' hive variable is available because beforeRoute has been run 


     // Set the page title from the dictionary file 
     $this->f3->set('pageTitle', $this->f3->get('DICT_'.'page_whatever')); 
     // Render the View 
     $this->f3->set('view','page.whatever.htm'); 
     $template=\Template::instance(); 
     echo $template->render('layout.sidebar.htm'); 

    } 

// End of Controller 
} 

,當然還有,在模板:

<repeat group="@categories" value="@category"> 
    <li> 
    <a href="{{ @category.uri }}">{{ @category.label }}</a> 
    </li> 
</repeat> 

PS:你可能會使用$f3代替$this->f3(和同爲$db

相關問題