2013-11-14 33 views
1

我正尝试拿起与parse_url$_SERVER['REQUEST_URI']正确的路径,以显示正确的网页,但是有我可是问题是只有我index.php页展示。

的URL,我index.php页面应该localhost/mvc-framework这是正确的,但是当我走: localhost/mvc-framework/file.php它也呈现出我index.php。基本上任何我在一些文件夹后键入也显示index.php页...

我真的很希望有人可以帮助我,因为我已经挣扎了近3洞天。 I'm大概愚蠢:/

我的所有文件“MVC的框架”和里面我有一个index.php文件捆绑在一起的一切,3个文件夹:“控制器”,“模式”,“意见”。

在控制器文件夹我已经一个文件名为main.php这是我正尝试拿起网址:

<?php 
/* controller/main.php 
*/ 
class mainController 
{ 
    public $load; 
    public $urlValues; 

    public function __construct() 
    { 
     $url = parse_url($_SERVER['REQUEST_URI']); 
     $url = explode('/', $url['path']); 
     $this->urlValues = array('controller' => $url[1]); 

     //index page 
     if ($this->urlValues['controller'] == "mvc-framework") { 

     $text = array("key" => "Hello"); 

     $this->load = new load(); 
     $this->load->view('index.php', $text); 
     } 

     //register page 
     elseif ($this->urlValues['controller'] == "mvc-framework/test.php") { 

     $text = array("key" => "Test"); 

     $this->load = new load(); 
     $this->load->view('test.php', $text); 
     } else { 
      echo 'error'; 
     } 

    }   
} 

在“模式”文件夹我有一个文件名为load.php这将采取URL,并将其指向的意见文件夹中的正确的文件...

<?php 
/* model/load.php 
*/ 
class load 
{ 

    /* This function takes parameter 
    * $file_name and match with file in views. 
    */ 

    function view($file_name, $data = null) 
    { 
     if (is_readable('views/' . $file_name)) { 
      if (is_array($data)) { 
       extract($data); 
      } 

      require 'views/' . $file_name; 
     } else { 
      echo $this->file; 
       die ('404 Not Found'); 
     } 
    } 

} 

在视图文件夹我有2个文件,一个index.php文件和一个test.php的...

这是我的嘘声tstrap/index.php文件是在根文件夹,并绑定在一起的一切......

<?php 
/* index.php 
*/ 

require_once 'model/load.php'; 
require_once 'controller/main.php'; 

new mainController(); 

回答

1

试试这个

$ URL =爆炸( '/',修剪($网址[ '路径'], '/'));

和这个 - $> urlValues [ '控制器'] == 'test.php的' 的请求本地主机/ MVC的框架/ test.php的

+0

OMG !!!!!!!!谢谢!就是这样!我没有认为修剪是正确的解决方案,因为我认为它删除了路径...... – Lisa

+0

只是一个问题,我不得不改变$ this-> urlValues ['controller'] =='index.php'以及为了获得索引页面加载。如何在不输入index.php后只进入localhost/mvc-framework /文件夹时显示index.php? – Lisa

+0

如果为空或index.php为索引控制器,则可以检查$ url [1]。或者你想看到url中的index.php?如果是,请尝试重定向到localhost/mvc-framework/index.php – webmcheck

0

你总是得到.htaccess文件的index.php文件怎么一回事,因为,它总是将请求重定向到前端控制器, index.php文件。我想...

+0

嗨!感谢您的评论!网址不会改变,但如果我输入另一个文件,它在url中看起来确定...这就是我在.htaccess文件中的内容:RewriteEngine on RewriteRule!\。(js | gif | jpg | png | css) $ index.php – Lisa

+0

这个规则(RewriteRule!\。(js | gif | jpg | png | css)$ index.php)表示没有扩展名的所有东西都应该被重定向到index.php;尝试请求file.php或file.css,并且你会注意到不同之处。 – ilpaijin

+0

但它指向我的index.php文件在根文件夹中。将所有内容绑定在一起的文件...显示的文件是我的index.php文件在视图文件夹内... – Lisa