2015-01-21 11 views
0

我想在我的主應用程序中創建一個子文件夾,但仍然允許獨立應用程序訪問smarty框架。使用Smarty PHP並需要子系統(獨立)

我已經創建了名爲「admin」的子文件夾。但是我不知道是否需要將它包含在我的設置文件中。

<?php 
require_once(SMARTY_DIR . 'Smarty.class.php'); 

class Smarty_EVENT extends Smarty { 

    function __construct() 
    { 

    // Class Constructor. 
    // These automatically get set with each new instance. 

    parent::__construct(); 

    $this->setTemplateDir('templates/'); 
    $this->setCompileDir('templates_c/'); 
    $this->setConfigDir('configs/'); 
    $this->setCacheDir('cache/'); 

    $this->caching = Smarty::CACHING_LIFETIME_CURRENT; 
    $this->assign('app_name', 'EVENT'); 
    } 

} 
?> 
+0

如果你是在子文件夾中,然後將'../'添加到您的路徑中。所以像'../ smarty/Smarty.class.php'或'../'。 SMARTY_DIR .'Smarty.class.php' – Forien 2015-01-21 09:45:39

+0

爲什麼使用文件夾而不是路線? (看看Slim框架,它與smarty很好地配合) – 2015-01-21 11:26:36

回答

-1

命名空間是包含其他類的更好方法。
命名空間的優點(主要的東西):

  • 沒有鬥爭包括/需要
  • 只是打字類的路徑來加載它
  • 同名不同類別
+0

'include' /'require'與命名空間無關。您可以擁有名稱空間並手動包含這些文件,並且可以在不具有名稱空間的情況下自動加載。 *上的關鍵字只是鍵入路徑,PHP神奇地發現它「*是['autoloader'](http://php.net/autoload)。它不會與PHP捆綁在一起。你必須編寫它,或者如果你的項目使用['Composer'](https://getcomposer.org/),那麼'Composer'會爲你寫。 – axiac 2015-01-23 16:10:33

+0

你說得對。命名空間是包含/需求的「進化」。所以這取決於你是否使用它。 但是,在這種情況下,當您需要包含類時,最好使用命名空間而不是include/require。 – 2015-01-26 10:47:58