2017-07-04 42 views
1

我想加載一個JavaScript和CSS文件的鏈接和腳本標籤。該項目使用php命名空間,因此文件正嘗試通過設置控制器的路由器腳本加載。也許需要更多的解釋。PHP命名空間加載JavaScript文件錯誤

當訪問者第一次來到http://site0.htaccess文件重寫到/public目錄與文件/public/.htaccess一切改寫爲/public/index.php文件。 /public/index.php只是一個spl_autoload函數,並調用router.class.php腳本來決定調用哪個控制器類。

root .htaccess內容:的public/.htaccess

<IfModule mod_rewrite.c> 
RewriteEngine On 

RewriteBase/

#RewriteCond %{REQUEST_URI} \.css$ 
RewriteRule (styles/[^/]+\.css)$ /$1 [L] 

# File is located in /foldername/images/ 
# Internal redirect. Does not change address in the browsers address bar. 
RewriteCond %{REQUEST_URI} \.(gif|jpg|png)$ 
RewriteRule ^.*(images)/(.*)(\.gif|jpg|png)$ $1/$2$3 [L] 

# File is located in /foldername/scripts/. 
# Internal redirect. Does not change address in the browsers address bar. 
RewriteCond %{REQUEST_URI} \.js$ 
RewriteRule (scripts/[^/]+\.js)$ /$1 [L] 

RewriteRule ^$ public/ [L] 
RewriteRule (.*) public/$1 [L] 
</IfModule> 

內容:中public/index.php

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)$ index.php?url=$1  [PT,L] 

</IfModule> 

內容:

<? 
//set_include_path('PATH_TO_GLOBAL_LIBS; PATH_TO_LIBRARY_1; PATH_TO_LIBRARY_X; PATH_TO_DOCUMENT_ROOT'); 

define('DS', DIRECTORY_SEPARATOR); 
define ('ROOT', dirname(dirname(__FILE__))); 

include_once (ROOT . DS . 'inc/dbconf.php'); 

function multi_autoloader($class) 
{ 
    $class = preg_replace('/^((\w*)\\\){2}/', '', $class); 

    $file = ROOT . DS . str_replace('\\', DS, strtolower($class)) . '.class.php'; 

     if (file_exists($file)) 
     { 
      require ($file);   
     } 
} 

spl_autoload_register('multi_autoloader'); 

use potts\john\libs\router\Router as Router; 

$router = Router::getInstance(); 
$router->initRouter(); 

router.class.php文件是相當大的,但如果你需要看它, 讓我知道。通過console/inspector我從css和javascript文件中獲得狀態500。在一個我不需要使用php namspaces的項目中,所有的工作都很好。所以這必須是由空隙造成的問題。

有關如何解決此問題的任何建議?

回答

0

這是一個醜陋的黑客,但它的工作原理:

/** 
* Sets the appropriate controller 
* 
* 
* @return object $this->controller 
*/ 

private function setController() 
{ 

    $this->_seo_uri = $this->seoUrl(); 

    $this->__controller = array_shift($this->_seo_uri); 

     if ($this->__controller == 'styles') 
     { 
       /* dev purposes only. php will return error that gets 
        appended to end of file. 
       */ 
      ini_set('display_errors',0); 

      /* include will return file with mime type 'text/html 
      thus not interpreting file as stylesheet 
      */ 
      header('Content-type: text/css'); 

      // readfile works too. your choice... 
      //readfile($_SERVER['DOCUMENT_ROOT']. '/styles/style.css'); 

      include ($_SERVER['DOCUMENT_ROOT']. '/styles/style.css'); 


     } 
     elseif ($this->__controller == 'scripts') 
     { 
       /* dev purposes only. php will return error that gets 
        appended to end of file. 
       */ 
      ini_set('display_errors',0); 

      include ($_SERVER['DOCUMENT_ROOT']. '/scripts/gui.js'); 


     } 
     else 
     { 

      $this->_controller = ucfirst($this->__controller); 

      $this->_controller = $this->_controller . "_Controller"; 


      $this->_base_namespace = '\bizdir\bizvend\controllers\\' . $this->__controller . '\\' . strtolower($this->_controller); 

       //use $this->_base_namespace as $this->_controller; 

      $this->controller = new $this->_base_namespace; 

      return $this->controller; 


     } 


} 

指其中我測試$此 - > __控制器無論是樣式或腳本。發生了什麼事情是在鏈接和腳本標記中將目錄(/ styles /或/ scripts /)設置爲命名空間中的控制器,並且路由器類嘗試查找Styles_Controller類或Scripts_Controller類。當我達到這一點時,我很可能還必須測試圖像。

希望這可以節省別人幾個小時的headbanging。

任何更清潔的方式......我接受建議。