使用 "pretty-url" 路由时我的 css 文件未加载
my css file not loading when using "pretty-url" routing
所以在我的应用程序中,假设 http://localhost:8888/mvc/user/login 这是一个 URL。
所以在这个 url 中,用户是我的控制器,登录是用户控制器中的方法,这在我的核心文件中编码如下:
<?php
/**
*
*/
class Bootstrap
{
function __construct()
{
$url = isset($_GET['url'])?$_GET['url']:null;
$url = rtrim($url,'/');
$url = explode('/', $url);
//print_r($url);
if (empty($url[0])) {
require 'controllers/welcome.php';
$controller = new Welcome();
$controller->index();
return false;
}
$file = 'controllers/'.$url[0].'.php';
if (file_exists($file)) {
require $file;
} else {
$this->showError();
}
$controller = new $url[0];
//calling methods
if (isset($url[2])) {
if (method_exists($controller, $url[2])) {
$controller->{$url[1]}($url[2]);
} else {
$this->showError();
}
} else {
if(isset($url[1])){
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}();
} else {
$this->showError();
}
} else {
$controller->index();
}
}
}
public function showError() {
require 'controllers/CustomError.php';
$controller = new CustomError();
$controller->index();
return false;
}
}
现在我的问题是,当我尝试在视图中加载 css 文件时,我的 URL 变成这样:
http://localhost:8888/mvc/public/css/style.css
所以现在根据上面的代码,假设public是一个控制器,CSS是一个方法。它给出了一个错误。
那么我该如何解决这个问题呢?
我希望你们明白问题出在哪里。
谢谢。
在视图的 head 部分为 CSS 文件尝试以下操作:
<link rel="stylesheet" type="text/css" href="http://localhost:8888/mvc/public/css/style.css">
所以在我的应用程序中,假设 http://localhost:8888/mvc/user/login 这是一个 URL。 所以在这个 url 中,用户是我的控制器,登录是用户控制器中的方法,这在我的核心文件中编码如下:
<?php
/**
*
*/
class Bootstrap
{
function __construct()
{
$url = isset($_GET['url'])?$_GET['url']:null;
$url = rtrim($url,'/');
$url = explode('/', $url);
//print_r($url);
if (empty($url[0])) {
require 'controllers/welcome.php';
$controller = new Welcome();
$controller->index();
return false;
}
$file = 'controllers/'.$url[0].'.php';
if (file_exists($file)) {
require $file;
} else {
$this->showError();
}
$controller = new $url[0];
//calling methods
if (isset($url[2])) {
if (method_exists($controller, $url[2])) {
$controller->{$url[1]}($url[2]);
} else {
$this->showError();
}
} else {
if(isset($url[1])){
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}();
} else {
$this->showError();
}
} else {
$controller->index();
}
}
}
public function showError() {
require 'controllers/CustomError.php';
$controller = new CustomError();
$controller->index();
return false;
}
}
现在我的问题是,当我尝试在视图中加载 css 文件时,我的 URL 变成这样:
http://localhost:8888/mvc/public/css/style.css
所以现在根据上面的代码,假设public是一个控制器,CSS是一个方法。它给出了一个错误。 那么我该如何解决这个问题呢?
我希望你们明白问题出在哪里。 谢谢。
在视图的 head 部分为 CSS 文件尝试以下操作:
<link rel="stylesheet" type="text/css" href="http://localhost:8888/mvc/public/css/style.css">