2011-03-10 11 views
-1

我建立自己的網站,這是我的頁面控制器,如何改進一個具有太多重複性變量的類?

/** 
* this file handles the retrieval and serving of page 
*/ 
class controller_core 
{ 
    public $connection = null; 
    public $page_model = null; 
    public $authentication_admin = null; 
    public $authentication_member = null; 
    public $constant = null; 

    public function __construct($connection) 
    { 
     $this->connection = $connection; 
     $this->page_model = new __page_model($connection); 
     $this->authentication_admin = new __authentication_admin($connection); 
     $this->authentication_member = new __authentication_member($connection); 
     $this->constant = new __constant_model($connection); 
    } 

    public function render_page($authenticated_admin, $authenticated_member, $backbone) 
    { 
     # ob_start - Turn on output buffering. It lets you put output into a buffer instead of sending it directly to the client. 
     # in computing, a buffer is a region of memory used to temporarily hold data while it is being moved from one place to another. 
     ob_start(); 

     # set variable for users 
     $user_primary = null; 
     $user_secondary = null; 
     $user_tertiary = null; 
     $user_quartary = null; 

     # set variable for members 
     $member_primary = null; 
     $member_secondary = null; 
     $member_tertiary = null; 

     # store the db connection object in the variable 
     $connection = $this->connection; 

     # determine it is a root user or a sub user from the class of authentication_admin 
     $category_admin = $this->authentication_admin->get_admin_category($authenticated_admin); 
     $category_member = $this->authentication_member->get_member_category($authenticated_member); 

     # set either one of the user type to true 
     switch($category_admin) 
     { 
      case 'user_primary': 
       $user_primary = true; 
       break; 
      case 'user_secondary': 
       $user_secondary = true; 
       break; 
      case 'user_tertiary': 
       $user_tertiary = true; 
       break; 
      case 'user_quartary': 
       $user_quartary = true; 
       break; 
     } 

     # set either one of the user type to true 
     switch($category_member) 
     { 
      case 'member_primary': 
       $member_primary = true; 
       break; 
      case 'member_secondary': 
       $member_secondary = true; 
       break; 
      case 'member_tertiary': 
       $member_tertiary = true; 
       break; 
     } 

     # get the constant values from the class of constant 
     $cst_value_site_title = $this->constant->get_constant('site_title')->cst_value; 
     $cst_value_site_slogan = $this->constant->get_constant('site_slogan')->cst_value; 
     $cst_value_meta_description = $this->constant->get_constant('meta_description')->cst_value; 
     $cst_value_meta_keywords = $this->constant->get_constant('meta_keywords')->cst_value; 


     # if $_REQUEST pg exists 
     if(isset($_REQUEST['pg_url'])) 
     { 
      # show the requested page 
      # always send the value of $authentication_admin to the class of page: 
      # if $authentication_admin has a value, you can see this page even if it is hidden 
      # if $authentication_admin has a value, you can see this page only if it is published 
      $page = $this->page_model->get_page($_REQUEST['pg_url'],$category_admin); 
      $parent = $this->page_model->get_parent($page); 

      # store the date into the variable 
      $parent_id = $page->parent_id; 
      $tmp_path = $page->tmp_path; 

      # get the main template/ html document 
      include $backbone; 
      //print_r($authentication_admin); 
     } 
     else 
     { 
      # if no special page is requested, we'll show the default page 
      $page = $this->page_model->get_page(DEFAULT_PAGE,$category_admin); 
      $parent = $this->page_model->get_parent($page); 

      #store the date into the variable 
      $parent_id = $page->parent_id; 
      $tmp_path = $page->tmp_path; 


      #get the main template/ html document 
      include$backbone; 
      #print_r($parent); 
     } 

     #Return the contents of the output buffer. 
     return ob_get_contents(); 

     #Clean (erase) the output buffer and turn off output buffering. 
     ob_end_clean(); 
    } 
} 
下面

的是,從上述父控制器類擴展的類,但你可以看到,我從重複一些(很多!)的變量父類,

class controller_extended extends controller_core 
    { 
     function __construct($connection) 
     { 
      parent::__construct($connection); 
     } 

     public function render_page($authenticated_admin, $authenticated_member, $backbone) 
     { 
      # set variable for users 
      $user_primary = null; 
      $user_secondary = null; 
      $user_tertiary = null; 
      $user_quartary = null; 

      # set variable for members 
      $member_primary = null; 
      $member_secondary = null; 
      $member_tertiary = null; 

      # store the db connection object in the variable 
      $connection = $this->connection; 

      # determine it is a root user or a sub user from the class of authentication_admin 
      $category_admin = $this->authentication_admin->get_admin_category($authenticated_admin); 
      $category_member = $this->authentication_member->get_member_category($authenticated_member); 

      # if $_REQUEST tag_name exists 
      if(isset($_REQUEST['tag_name'])) 
      { 
       # get the value from the request 
       if(isset($_REQUEST['pg_url'])) $pg_url = $_REQUEST['pg_url']; 
       if(isset($_REQUEST['tag_name'])) $tag_name = $_REQUEST['tag_name']; 
       if(isset($_REQUEST['str_id'])) $str_id = $_REQUEST['str_id']; 

       # show the requested page 
       # always send the value of $authentication_admin to the class of page: 
       # if $authentication_admin has a value, you can see this page even if it is hidden 
       # if $authentication_admin has a value, you can see this page only if it is published 
       $page = $this->page_model->get_page($pg_url,$category_admin); 
       $parent = $this->page_model->get_parent($page); 

       if(empty($str_id)) 
       { 
        # get the included template 
        switch($pg_url) 
        { 
         case 'publications': 
          $tmp_path = 'resources_publication_subitem.php'; 
          break; 
         case 'tender-opportunities': 
          $tmp_path = 'resources_tender_opportunitie_subitem.php'; 
          break; 
         case 'research-topics': 
          $pg_url = $tag_name; 
          $tmp_path = 'item_content_research_topics.php'; 
          break; 
         case 'videos': 
          $tmp_path = 'video_tagged.php'; 
          break; 
         case 'forum': 
          $tmp_path = 'forum_subitem.php'; 
          break; 
         case 'ener-exchange': 
          $tmp_path = 'exchange_subitem.php'; 
          break; 
        } 
       } 
       else 
       { 
        # store the date into the variable 
        $parent_id = $page->parent_id; 

        # get the included template 
        switch($pg_url) 
        { 
         case 'forum': 
          $tmp_path = 'item_forum.php'; 
          break; 
         case 'ener-exchange': 
          $tmp_path = 'item_exchange.php'; 
          break; 
        } 

       } 

       # get the main template/ html document 
       include $backbone; 

      } 
      else 
      { 
       parent::render_page($authenticated_admin, $authenticated_member, $backbone); 
      } 
     } 
    } 

我該如何解決這個重複性變量?也許我錯誤地製造了控制器?

謝謝。

編輯:

對不起,沒有說清楚。在我的擴展類中,render_page的方法重寫了父類中的render_page方法,所以我認爲我無法使用parent關鍵字來獲取存儲在render_page方法中的變量。我應該怎麼做?謝謝。

+0

爲什麼重新發明輪子? – dynamic 2011-03-10 15:18:26

+1

MVC並不意味着「我的代碼」。這種災難與MVC架構模式完全不相關。 – 2014-09-07 08:33:00

+0

@tereško爲什麼敵對的語氣?我同意這裏的問題是不知情/錯誤的,但OP有可能試圖儘可能地理解框架共有的更大模式。這種態度不具有建設性。 – DeaconDesperado 2014-09-09 09:02:18

回答

-1

對於部署這些變量時想要完成的部分內容,很難得到一個想法,但僅僅從看看這裏有什麼可以考慮的事情。

您可以將用於確定用戶級別的開關置於父類的受保護方法中。這樣,檢查用戶的級別的邏輯可以由任何後代控制器中的render_page調用。您可以傳入get_admin_category中的值並返回一個字符串,以便switch語句可以重用。

另外,你的例子中輸出緩衝背後的意圖是什麼?我發現你的模板包含最後的include $backbone,但是你爲什麼要做緩衝?在大多數發佈的MVC中,控制器會進行一些預處理,調用模型方法並排列數據,然後將其傳遞給視圖,該視圖是呈現給客戶端的唯一視圖(例如,我知道CodeIgniter通過交付「輸出'數組到視圖,該視圖可以包含通過回顯等輸出的值)。應該不需要將輸出延遲到客戶端,因爲在大多數MVC中,只有在視圖加載後纔會出現這種情況。

從你使用$ _REQUEST超全局的外觀來看,你依靠這個來執行請求到服務器上的實際腳本的路由(我可能會誤解它,只是試圖從給出的內容中解釋)。 MVC還將利用mod_rewrite()執行所有路由到一個入口點(前端控制器),然後依靠腳本來處理所有路由,方法是調用url構造器中的路徑。這是一個美妙的教程進行鍛鍊,不部署,用途: http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/

如果你這樣做是爲了解Front Controller模式的練習,有很多偉大的框架,在那裏你可以看看和學習一些技巧來自。 CodeIgniter和Kohanna都是很好的,相對輕量級的,可以很容易地看到底下的外觀。很顯然,Zend也很受歡迎。

+0

感謝您的回覆。我不知道緩衝 - 我認爲它可以使網站更快加載 - 我想我應該擺脫它!謝謝! – laukok 2011-03-10 15:12:47

+0

@lauthiamkok我添加了一個關於mod_rewrite路由的好教程的鏈接。 – DeaconDesperado 2011-03-10 15:15:02

+0

感謝編輯,執事。 – laukok 2011-03-10 15:22:29

相關問題