创建一个php bootstrap文件 - 无法访问文件

时间:2011-08-13 13:55:33

标签: php apache .htaccess bootstrapping

我试图在php中创建一个小引导程序。

我的伙伴是这样的:

./application
./application/_styles
./application/_img
./application/views
./application/views/index
./application/views/error
./application/controllers/
./application/models/
./application/Bootstrap.php
./.htaccess

的.htaccess

Options +FollowSymLinks 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ application/Bootstrap.php [NC,L]

bootstrap.php中

<?php
#set up the project to developing state
error_reporting(E_ALL);
ini_set('display_errors','On');

#let's set up a root path constant
define('ROOT',getcwd().DIRECTORY_SEPARATOR);
#useful conf.
define('IMG', ROOT.'views/_img/');
define('CSS', ROOT.'views/_styles/');

$projectUrl = "http://www.neophp.com/";
$siteUrl = "http://www.neophp.com";

class neoPHP{

    #== Method to get current URL request ==#
    private function zinit(){
        #get variables
        $host = $_SERVER['HTTP_HOST'];
        $self = $_SERVER['REQUEST_URI'];
        $query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
        $url = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
        #sort data
        $request = str_replace($GLOBALS['projectUrl'], "", $url);
        $request = explode("/",$request);
        $request = array_filter($request);
        #make readable
        $load = array();
        foreach($request as $rq){
            $load[] = $rq;
        }
        #return information
        return $load;
    }

    public function getClasses(){
         // create an array to hold directory list
        $results = array();
        // create a handler for the directory
        $handler = opendir(ROOT.'models/');
        // open directory and walk through the filenames
        while ($file = readdir($handler)) {
          // if file isn't this directory or its parent, add it to the results
          if ($file != "." && $file != ".." && strstr($file, '.php')) {
            $results[] = $file;
          }
        }
        // tidy up: close the handler
        closedir($handler);
        // done!

        foreach( $results as $class ){
            include_once (ROOT.'models/'.$class);
        }
    }    

    #== Method to load requred pages ==#
    public function zinitLoad(){
        $this->getClasses();
        $items = $this->zinit();
        #include all models
        global $neo;
        global $users;

            if(sizeof($items) == 0) {
                if (file_exists(ROOT.'controllers/index/index.php') && file_exists(ROOT.'views/index/index.phtml')){
                    include_once(ROOT.'controllers/index/index.php');
                    include_once(ROOT.'views/layout/layout.phtml');
                    include_once(ROOT.'views/index/index.phtml');
                    include_once(ROOT.'views/layout/footer.phtml');
                } else {header('Location: '.$GLOBALS['projectUrl'].'error/');}
            }elseif(sizeof($items) == 1) {
                if (file_exists(ROOT.'controllers/'.$items['0'].'/index.php') && file_exists(ROOT.'views/'.$items['0'].'/index.phtml')){
                    include_once(ROOT.'controllers/'.$items['0'].'/index.php');
                    include_once(ROOT.'views/layout/layout.phtml');
                    include_once(ROOT.'views/'.$items['0'].'/index.phtml');
                    include_once(ROOT.'views/layout/footer.phtml');
                } else {header('Location: '.$GLOBALS['projectUrl'].'error/');}

            } 
            elseif (sizeof($items >= 2)){
                 if (file_exists(ROOT.'controllers/'.$items['0'].'/'.$items[1].'.php') && file_exists(ROOT.'views/'.$items['0'].'/'.$items[1].'.phtml')){
                    include_once(ROOT.'controllers/'.$items['0'].'/'.$items[1].'.php');
                    include_once(ROOT.'views/layout/layout.phtml');
                    include_once(ROOT.'views/'.$items['0'].'/'.$items[1].'.phtml');
                    include_once(ROOT.'views/layout/footer.phtml');
                 } else {header('Location: '.$GLOBALS['projectUrl'].'error/');}
            }
    }

    #== Method to print arrays ==#
    public function show($arr){
        echo '<pre>';print_r($arr);echo '</pre>';
    }

}


$neo = new neoPHP();
$neo->getClasses();

$neo->zinitLoad();
//print_r(getDirectoryList(ROOT));
//if (class_exists('Users')) echo 'exists';



?>

我的问题在于HTML代码。当我尝试调用* .css文件或* .png文件时,脚本将返回错误页面。我想

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

将解决这个问题,但它没有。

所以我需要一些帮助/建议来访问使用此设置的文件或任何其他建议。

1 个答案:

答案 0 :(得分:2)

似乎我添加了一个新的重写规则,如此启用了对文件的访问权限:

Options +FollowSymLinks 
RewriteEngine On
Rewriterule ^application/views/.*$ - [PT]
RewriteRule ^.*$ application/Bootstrap.php [NC,L]