2017-07-04 53 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。

任何更清洁的方式......我接受建议。