创建 php 引导文件——无法访问文件

发布于 2024-11-29 04:43:10 字数 4804 浏览 2 评论 0原文

我正在尝试在 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

可以解决这个问题,但事实并非如此。

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

I`m trying to create a small bootstrap in php.

my dirs are like this:

./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';



?>

My problem is with HTML code. When i try to call to a *.css file or *.png file the script returns to error page. I thought

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

will solve this problem, but it doesn`t.

So I need some help/advice with accessing files with this setup, or any other advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2024-12-06 04:43:10

似乎如果我添加一个新的重写规则,例如启用对文件的访问:

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

Seems if I add a new Rewrite rule like this access to files is enabled:

Options +FollowSymLinks 
RewriteEngine On
Rewriterule ^application/views/.*$ - [PT]
RewriteRule ^.*$ application/Bootstrap.php [NC,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文