我正尝试拿起与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();
OMG !!!!!!!!谢谢!就是这样!我没有认为修剪是正确的解决方案,因为我认为它删除了路径...... – Lisa
只是一个问题,我不得不改变$ this-> urlValues ['controller'] =='index.php'以及为了获得索引页面加载。如何在不输入index.php后只进入localhost/mvc-framework /文件夹时显示index.php? – Lisa
如果为空或index.php为索引控制器,则可以检查$ url [1]。或者你想看到url中的index.php?如果是,请尝试重定向到localhost/mvc-framework/index.php – webmcheck