2012-12-19 32 views
10

我太困了,當我問這個問題,所以很抱歉,無論如何要澄清事情我準備了2個小時的問題。初始化視圖,模板和控制器和模型是可選的

我想整理我的代碼,並決定組織它mvc'ish(MVC-等),我不知道我是否能遵守所有的原則,但我想至少是接近對此。

我的應用程序有一個前端控制器(不知道如果我的定義是正確的),這樣我的應用程序的所有http請求都會通過一個點,在我的應用程序根目錄下的index.php

說了這樣的話,你可以想象我用.htaccess來指示所有的請求到index.php

我分解了url並創建了一個數組,其中$url[]就像這樣。所以每當我訪問我的應用程序是這樣http://localhost/app/pagename這將是訪問控制器(pagename_controller

我做了這樣的:

$file = $controller_path . $page . '_controller.php'; 

if (file_exists($file)) { 
    require $file; 
    $class_name = ucfirst($page) . '_controller'; 
    $target = new $class_name(); 
} 

還我包起來在一個集裝箱中,「裝飾圖案」 ,以供將來使用,驗證也許。 這樣的:

$controller = new Wrap($target); 
$controller->index(); 

我不知道,如果使用$controller變量名是合適的,所以請原諒我當它是完全錯誤的。

我還挺想,我可以設置我的應用程序是這樣的:

  1. 用戶發送請求,怎麼樣?通過使用該應用程序意味着他/她發出一個HTTP請求,將加載應用

    credit : phparchitect design patterns

的初始狀態如你我的期望的應用結構的圖中看到,我只能做的第一部分是將請求引導到單個條目(index.php

現在的問題是應用程序的其他部分的初始化。

截至目前,我有3個文件,我想要設置,但我很困惑如何。

index_controllerindex_viewTemplate

class Index_controller { 
    private $model; 
    private $view; 

    public function __construct(){ 
     // optional model -> $this->model = 'index' 
     $this->view = 'index' // 
    } 

    public function index(){ 
     $this->load->view($this->view) 
    } 
} 

class Index_view { 
    private $model; 
    private $template; 

    public function __construct(Model $model = null){ 
     $this->template = new Template('default'); 
    } 

    public function view() { 
     $this->template->assign('css', 'default_css'); // don't know if this is efficient 
     // or $this->template->assign('header', 'default_header'); 
     // or $this->template->assign('sidebar', 'default_sidebar'); 
     // or $this->template->assign('footer', 'default_footer'); 
     // or any other things I want to use in the template 
    } 
} 

class Template { 

    public $data = array(); 
    private $tmpl; 

    public function __construct($template) { 
     $this->tmpl = $template . '_tmpl.php'; 
    } 

    public function assign($name, $value){ 
     $this->data[$name] = $value; 
    } 

    // public function output 
    // function that will explode the data array and render it out as a webpage 
    // I'll create templates and 
} 

與在手,現在我想知道我怎麼這些事情聯繫在一起。目前我有一個可以包含類的system文件夾,並且我爲該文件夾設置了一個自動加載器。

我想創建一個Controller類和View類,它充當ActionFactory和ViewFactory,如圖所示,儘管我知道這些不是他們的責任。

我想這:

class Controller { 

    protected $load; 
    public function __construct() { 
     $this->load = new View(); 
    } 
} 

class View { 
    public function __construct() { 
    // some things i don't know 
    } 

    public function view() { 
    // some things i don't know 
    } 
} 

你有什麼建議,我設置的意見。我怎樣才能啓動黑社會?

+0

目前尚不清楚你的實際問題是什麼。你能否更新你的帖子來問一系列具體問題? – Charles

+0

編輯,我說得很清楚,我也試過了 –

+0

@JoeySalacHipolito你在驗證'$ page'嗎?如果沒有,你有一個本地文件包含漏洞,'file_exists()'不會阻止。 – MrCode

回答

1

好吧,讓我們不要太拘泥於你的實現細節。您可以在其他時間閱讀關於保護您的框架的內容。讓我們來處理你的問題...

我實際上創建了一個框架,沿着你正試圖實現的路線工作。我認爲你缺少的是一個RoutingHandler類。路由是URL的物理操縱,它告訴您的應用程序加載哪個Controller以及要運行哪個Action。

在我的世界我也有模塊,因此基本的路由方案是

Module -> Controller -> Action 

這三項映射到以那種方式我的URI方案。變量可以附加也像這樣......被解析的URI後

http://www.domain.com/module/controller/action/var1/val1/var2/val2

那麼,會發生什麼,並且控制傳遞到適當的控制器和行動?讓我們做一些代碼,使其表現出一個簡單的例子...

<?php  
    class indexController extends Controller { 

     protected function Initialize() { 
      $this->objHomeModel = new HomeModel; 

      $this->objHeader = new Header(); 
      $this->objFooter = new Footer(); 

      $this->objHeader 
       ->SetPageId('home'); 
     } 

     public function indexAction() { 
      $this->objHeader->SetPageTitle('This is my page title.'); 
     } 
    } 
?> 

Initialize方法,我設置了一些控制範圍內的東西,抓住我的模型的實例以供以後使用。真正的肉在indexAction方法。這是您將在View中設置要使用的內容的位置。例如...

public function randomAction() { 
    $this->_CONTROL->Append($intSomeVar, 42); 
} 

_CONTROL是我操縱和傳遞到查看值的陣列。 Controller類知道如何爲View查找正確的模板,因爲它是以Action(和在同級目錄中)命名的。

Controller父類所執行的操作方法的名稱,並解析它像這樣...

indexAction -> index.tpl.php 

你也可以做一些其他有趣的東西在這裏,例如...

Application::SetNoRender(); 

...會告訴控制器不要在模板內渲染,但只需完成該方法。這對於實際上不想輸出任何內容的情況很有用。

最後,所有的控制器,模型和視圖住自己的目錄,像這樣的內部...

my_module 
    controllers 
     indexController.class.php 
     someotherController.class.php 
     : 
     : 
    models 
     HomeModel.class.php 
     : 
     : 
    templates 
     index.tpl.php 
     someother.tpl.php 
     : 
     : 

我可以繼續下去,但我從內存中寫入這一點,並有一些皺紋在這裏和那裏,但希望這給你思考的食物。

+0

即使只是一個例子,你的'Initialise()'方法是創建一個新的對象左中心。這裏需要依賴注入或教育IoC原則作爲旁註。 – Jimbo

+0

一個字:「Singleton」,寶貝! –

+0

我希望你的意思是「永遠不要使用單身人士」。或者靜態方法;) – Jimbo