Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Kohana] Szkielet strony
Forum PHP.pl > Forum > PHP > Frameworki
marcinpruciak
Poznaję framwork Kohana. Napisałem sobie w nim system newsów. Ale jest to tylko zawartość strony czyli content (strona dzieli się na banner, menu, content i stopkę).

Jak napisać kontroler który stworzy szkielet strony. Doda kontroler do menu i wskarze miejsce w widoku dla contentu?
phpion
Template_Controller + podwidoki lub mechanizm komponentów.
marcinpruciak
Zrobiłem to w ten sposób.

Stworzyłem Site_Controller, który tworzy stronę i dołącza narazie tylko Menu_Conntroller.

  1. <?php
  2. class Site_Controller extends Template_Controller {
  3.  
  4.      public $template = 'base_page';
  5.  
  6.      public function __construct()
  7.  {
  8.      parent::__construct();
  9.      $this->template->title     = "Welcome to Kohana!";
  10.      $this->template->copyright = "Š Me, 2008";
  11.  
  12.      // Look:
  13.         $menu = new Menu_Controller;
  14.         $this->template->menu = $menu->_getMenu();
  15.  }
  16. ?>



Po klasie Site_Controller dziedziczy klasa News_Controller, w przyszłości będzie dziedziczyć każda strona znajdująca się w content.

  1. <?php
  2. class News_Controller extends Site_Controller {
  3.    // const ALLOW_PRODUCTION = FALSE;
  4.     public $AutoRender = TRUE;
  5.     public $aContent;
  6.  
  7.     public function __construct(){
  8.         parent::__construct();
  9.         /*if ($this->AutoRender == TRUE){
  10.             Event::add('system.post_controller', array($this, '_render'));
  11.         }*/
  12.     }
  13.  
  14.     public function _render(){
  15.         if ($this->AutoRender == TRUE){
  16.           $this->template->render(TRUE);
  17.         }
  18.     }
  19.  
  20.     public function index($iActive='all', $iSubactive = NULL){
  21.         $this->template->content = 'ddd';
  22.         //$this->_render();
  23.         //$this->_setContent($iActive, $iSubactive);
  24.     }
  25.     
  26.     public function one($iSubactive){
  27.         Benchmark::start('benchmark1');
  28.         $this->_setContent('one', $iSubactive);
  29.         Benchmark::stop('benchmark1');
  30.         print_r(Benchmark::get('benchmark1'));
  31.     }
  32.     
  33.     public function all(){
  34.         $this->_setContent('all', NULL);
  35.     }
  36.  
  37.     private function _setContent($iActive, $iSubactive){
  38.         $mContent = new Content_Model;
  39.         if($iActive == 'one'){
  40.             $this->template->content = new View('Content');
  41.             $this->aContent = $mContent->getOneContent($iSubactive);
  42.             $this->template->content->id = $this->aContent['id'];
  43.             $this->template->content->title = $this->aContent['title'];
  44.             $this->template->content->content = $this->aContent['content'];
  45.             if($this->aContent['comments']['num'] > 0){
  46.                 $this->template->content->numofComments = $this->aContent['comments']['num'];
  47.                 $this->template->content->comments = $this->aContent['comments']['content'];
  48.             }
  49.             $this->template->content->form_comments = new View('form_comments');
  50.             $this->template->content->form_comments->id = $this->aContent['id'];
  51.         }elseif($iActive == 'all'){
  52.             $this->template->content = new View('all');
  53.             $this->aContent = $mContent->getAllContent();
  54.             $this->template->content->news = $this->aContent['news'];
  55.             $this->template->content->numofComments = $this->aContent['num_of_comments'];
  56.         }
  57.     }
  58.  
  59.     public function add_comment($iSubactive){
  60.         $mForm = new Form_comment_Model;
  61.         if($mForm->receiveData()){
  62.             $this->_setContent('one', $iSubactive);
  63.             $this->template->form_comments->message = 'Dodano komentarz';
  64.         }else{
  65.             $this->_setContent('one', $iSubactive);
  66.             $this->template->form_comments->message = 'Wypełnij wszystkie pola w formularzu';
  67.         }
  68.     }
  69. }
  70. ?>



Jeszcze Menu_Controller

  1. <?php
  2. class Menu_Controller extends Template_Controller {
  3.  
  4.     public $template = 'menu';
  5.  
  6.     function __construct() {
  7.         parent::__construct();
  8.     }
  9.  
  10.     public function _getMenu(){
  11.         $menuModel = new Menu_Model;
  12.         $this->template->title = $menuModel->_getTitle();
  13.         $this->template->pages = $menuModel->_getPages();
  14.         $this->template->subpages = $menuModel->_getSubPages();
  15.     }
  16.  
  17. }
  18. ?>





Proszę o ocenę i czy dobrze myślę, ale wydaje mi się że tak.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.