Php MVC 模型 - 为什么上下文(路径)在相同 controller/view 中发生变化,当默认 home 参数被相同的替换时:url[1]
Php MVC Model - why the context (path) changes in the same controller/view when default home parameter is replaced by the same in: url[1]
我想在我的 php 项目中采用 MVC 模型。我已经创建了文件夹结构:模型、视图、控制器等...
我面临的问题:
如果我导航到:"www.mysite.com/home"(请注意,默认情况下会调用 'index' 方法),网站会加载资源(/css、/img 等)就好了。
但是,如果我调用:"www.mysite.com/home/index"(与默认值相同),我的资源将无法加载(即:它在:home/index/css 中查找 css,这确实不存在)...:(
我在 .htaccess 中重写了规则,所以我的 /index.php 中应该包含一些东西吧?为什么我的资源路径会改变?
我在我的应用程序中执行的步骤:
在我的核心App.php声明:
protected $controller = 'home';
protected $method = 'index';
protected $params = [];
默认情况下,这些页面通过以下方式打开:
public function __construct(){
$url = $this->parseUrl();
if(file_exists('app/controllers/' . $url[0] . '.php')){
$this->controller = $url[0];
unset($url[0]); //remove it from the array
}
require_once 'app/controllers/' . $this->controller . '.php';
$this->controller = new $this->controller;
if(isset($url[1])){
if(method_exists($this->controller, $url[1])){
$this->method = $url[1];
unset($url[1]);
}
}
$this->params = $url ? array_values($url) : [];
call_user_func_array([$this->controller, $this->method], $this->params);
}
public function parseUrl(){
if(isset($_GET['url'])){
return $url = explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
}
然后控制器 home.php 被调用 通过扩展核心控制器获取正确的 index.php 文件:
class Home extends Controller {
public function index($name = ''){
//$user = $this->model('User');
//$user->name = $name;
$this->view('home/index', []);
}
}
Controller.php
class Controller {
public function model($model){
require_once 'app/models/' . $model . '.php';
return new $model();
}
public function view($view, $data = []){
require_once 'app/views/' . $view . '.php';
}
.htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /home/prbh0pr/public_html/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url= [QSA,L]
文件夹结构:
如果您在控制器中,则需要 css/js 文件,例如,如果您在网络中。com/public/home
<script type="text/javascript" src="js/md/tinymce.min.js"></script>
如果home/index
<script type="text/javascript" src="../js/md/tinymce.min.js"></script>
我想在我的 php 项目中采用 MVC 模型。我已经创建了文件夹结构:模型、视图、控制器等...
我面临的问题:
如果我导航到:"www.mysite.com/home"(请注意,默认情况下会调用 'index' 方法),网站会加载资源(/css、/img 等)就好了。
但是,如果我调用:"www.mysite.com/home/index"(与默认值相同),我的资源将无法加载(即:它在:home/index/css 中查找 css,这确实不存在)...:(
我在 .htaccess 中重写了规则,所以我的 /index.php 中应该包含一些东西吧?为什么我的资源路径会改变?
我在我的应用程序中执行的步骤:
在我的核心App.php声明:
protected $controller = 'home';
protected $method = 'index';
protected $params = [];
默认情况下,这些页面通过以下方式打开:
public function __construct(){
$url = $this->parseUrl();
if(file_exists('app/controllers/' . $url[0] . '.php')){
$this->controller = $url[0];
unset($url[0]); //remove it from the array
}
require_once 'app/controllers/' . $this->controller . '.php';
$this->controller = new $this->controller;
if(isset($url[1])){
if(method_exists($this->controller, $url[1])){
$this->method = $url[1];
unset($url[1]);
}
}
$this->params = $url ? array_values($url) : [];
call_user_func_array([$this->controller, $this->method], $this->params);
}
public function parseUrl(){
if(isset($_GET['url'])){
return $url = explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
}
然后控制器 home.php 被调用 通过扩展核心控制器获取正确的 index.php 文件:
class Home extends Controller {
public function index($name = ''){
//$user = $this->model('User');
//$user->name = $name;
$this->view('home/index', []);
}
}
Controller.php
class Controller {
public function model($model){
require_once 'app/models/' . $model . '.php';
return new $model();
}
public function view($view, $data = []){
require_once 'app/views/' . $view . '.php';
}
.htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /home/prbh0pr/public_html/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url= [QSA,L]
文件夹结构:
如果您在控制器中,则需要 css/js 文件,例如,如果您在网络中。com/public/home
<script type="text/javascript" src="js/md/tinymce.min.js"></script>
如果home/index
<script type="text/javascript" src="../js/md/tinymce.min.js"></script>