我建立自己的網站,這是我的頁面控制器,如何改進一個具有太多重複性變量的類?
/**
* 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
方法中的變量。我應該怎麼做?謝謝。
爲什麼重新發明輪子? – dynamic 2011-03-10 15:18:26
MVC並不意味着「我的代碼」。這種災難與MVC架構模式完全不相關。 – 2014-09-07 08:33:00
@tereško爲什麼敵對的語氣?我同意這裏的問題是不知情/錯誤的,但OP有可能試圖儘可能地理解框架共有的更大模式。這種態度不具有建設性。 – DeaconDesperado 2014-09-09 09:02:18