【发布时间】:2016-05-01 01:55:37
【问题描述】:
我想使用 MVC 模式来划分我的逻辑,从表示和数据。
嗯,我一直在寻找我。但事实是,我什至不知道要搜索什么。
我正在尝试在 php 中设置 MVC 框架。我正在关注 youtube 上的教程,但我被困在路由点。
我读过很多指南,每一个都以不同的方式教授东西,只会造成更多的混乱。
重点是:
我有一个 .htaccess 文件,其中包含一些指令(但问题是我不知道所有这些指令的含义。我从不理解 htaccess 逻辑)
Options -MultiViews
RewriteEngine On
#I think this sets the base url of the site?
RewriteBase /~caiuscitiriga/mvc/public
#What does this mean??
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#AND THIS?!
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
然后我有这些 php 脚本:
索引.php
<?php
require_once '../app/init.php';
$app = new App();
init.php
<?php
require_once 'core/App.php';
require_once 'core/Controller.php';
App.php
不要问我为什么使用 filter_var 和 rtrim。因为这正是我想弄清楚的。正如我之前所说,这段代码不是我的。我确定它的技巧正是在 .htacess 和 App.php 中,但我不明白其中的逻辑
class App{
protected $controller = 'home';
protected $method = 'index';
protected $params = [];
public function __construct()
{
print_r($this->parseUrl());
}
public function parseUrl()
{
if(isset($_GET['url']))
{
return $url = explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
}
}
Controller.php
<?php
class Controller{
}
home.php
<?php
class Home extends Controller{
public function index()
{
echo 'home/index';
}
}
如果我通过此网址:localhost/~caiuscitiriga/mvc/public/home/index/maxine
我得到这个:数组([0] => home [1] => index [2] => maxine)
为什么?!!?我的意思是,它是正确的。但是为什么呢??
【问题讨论】:
-
为什么我要以这种特定方式获取该数组? url的htacess是什么?在那之后,在返回 url 之前 php 到底做了什么?
-
引用:“我从来没有理解过 htaccess 逻辑”...然后我建议你开始阅读文档而不是“大量指南”。
标签: php apache .htaccess apache2