2010-12-16 19 views
0

我一直在爲PHP靈感來自Pylons的最小MVC。我的控制器是這樣的:創建一個簡單的PHP MVC - 將全局變量公開給View的最佳方式是什麼?

<?php 
defined('__CORE__') or die('no direct access'); 

class Demo extends BaseController 
{ 
    public function hello($args) 
    { 
     return parent::render('hello_tmpl', array('foo' => 'bar')); 
    } 
} 

?> 

<?php 
defined('__CORE__') or die('no direct access'); 

class BaseController 
{ 
    public function render($template, $vars=FALSE) 
    { 
     // Load up some global variables to put into the template's scope 
     global $map; 

     if ($vars !== FALSE) 
      extract($vars); 

     // Load up the template file's contents and save to the $_body variable 
     // This variable will be utilized in base.html 
     ob_start(); 
     include VIEW . '/' . $template . '.phtml'; 

     $_body = ob_get_contents(); 
     ob_end_clean(); 

     require_once(VIEW . '/base.php'); 
    } 
} 

?> 

我遇到了麻煩,想出了一個很好的方法來暴露全局變量到視圖。 $map是另一個文件中定義的路由映射器。我希望能夠看到視圖和控制器,這樣我就可以利用$map->url_for('home_page')

這是幹什麼的?理想情況下,我想這是發生在BaseController,所以我可以做這樣的事情:

public function hellp($args) 
{ 
    $home_page = parent::$map->url_for(); 
} 

除了從視圖訪問$map

回答

相關問題