问题描述
我想使用MVC模式将我的逻辑从表示形式和数据中划分出来。
好,我一直在搜索我。但事实是,我什至不知道要搜索什么。
我正在尝试在php中设置MVC框架。我正在关注youtube上的教程,并且停留在路由点。
我已经阅读了很多指南,每个人都以不同的方式讲课,只会造成更多的混乱。
问题是这样的:
我有一个包含某些指令的.htaccess文件(但问题是我不知道所有这些指令的含义。我从不了解htaccess逻辑)
Options -MultiViews
RewriteEngine On
#我认为这可以设置网站的基本网址?
RewriteBase /〜caiuscitiriga / mvc / public
#这是什么意思?
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME}!-f
#这是?!
RewriteRule ^(。+)$ index.php?url = $ 1 [QSA,L]
然后我有以下php脚本:
Index.php
<?php
require_once'../app/init.php';
$ app =新App();
init.php
<?php
require_once'core / App.php';
require_once‘core / Controller.php’;
App.php
不要问我为什么使用 filter_var 和 rtrim 。因为这正是我要弄清楚的。如我之前所说,此代码不是我的。我确定诀窍完全在.htacess和App.php中,但我不理解逻辑
class App {
受保护的$ controller ='home';
保护$ method =索引;
受保护的$ params = [];
公共功能__construct()
{
print_r($ this-> parseUrl());
}
公共函数parseUrl()
{
if(isset($ _ GET ['url']))
{
return $ url = explode('/',filter_var(rtrim($ _ GET ['url'],'/'),FILTER_SANITIZE_URL));
}
}
}
Controller.php
<?php
class Controller {
}
home.php
<?php
类Home扩展控制器{
公共功能index()
{
echo'home / index';
}
}
如果我通过以下网址: localhost /〜caiuscitiriga / mvc / public / home / index / maxine
我得到了这个:数组([0] => home [1] => index [ 2] => maxine)
为什么?!?我的意思是,这是正确的。但是为什么呢?
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^(。+)$ index.php?url = $ 1 [QSA,L]
我将以上内容理解为:如果请求不是目录,也不是文件,则采用路径并将其以url属性作为路径在内部传递给index.php。 / p>
所以现在
// example.com/big/bad/妈妈
映射到
//example.com/index.php?url=大/坏/妈妈
您可以根据需要调用上面的脚本。
然后,您的解析网址就是url的值( big / bad / mamma),删除了如果有斜杠,则以斜杠结尾。然后在遇到正斜杠的任何地方分割字符串。因此,您将分为三个部分。
来自:
FILTER_SANITIZE_URL过滤器将删除除字母,数字和$ -_。+!*!()外的所有字符,{} | \ ^〜[]`<>#%; /?:@& =。
但是如果您想了解请分解片段:
$ url = $ _GET ['url'];
var_dump($ url);
$ url = rtrim($ url,'/');
var_dump($ url);
$ url = filter_var($ url,FILTER_SANITIZE_URL);
var_dump($ url);
$ url = explode('/',$ url);
var_dump($ url);
I want to use the MVC pattern to divide my logic, from the presentation and the data.
Well, i've been searching for i while. But the truth is that i don't even know what to search.
I'm trying to setup a MVC Framework in php. I'm following a tutorial on youtube, and i'm stuck at the routing point.
I've read a LOT of guides, and every single one teaches things in different ways, creating only more confusion.
The point is this:
i have a .htaccess file that contains some directives (but the problem is that i don't know what all those directives means. I've never understood the htaccess logic)
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]
And then i have these php scripts:
Index.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
Don't ask me why i used filter_var and rtrim. Because is exactly what i want to figure out. As i said before, this code isn't mine. I'm sure that the trick it's exactly in .htacess and App.php but i don't understand the logic
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';
}
}
If i pass this url: localhost/~caiuscitiriga/mvc/public/home/index/maxine
I GET THIS: Array ( [0] => home [1] => index [2] => maxine )
WHY?!!? I mean, it's correct. But why??
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I read the above as, if the request is not a directory, and not a file, then take the path and pass it to index.php internally with the url attribute as the path.
So now
//example.com/big/bad/mamma
maps to
//example.com/index.php?url=big/bad/mamma
You could call the script as above if you want.
Then your parse url is taking the value of url ('big/bad/mamma'), removing a trailing slash if there is one. And then splitting the string wherever it encounters a forward slash. So you end up with three parts. Which is what you have in your array.
From the manual:
The FILTER_SANITIZE_URL filter will remove all characters except letters, digits and $-_.+!*'(),{}|\^~[]`<>#%";/?:@&=.
But break it down if you want to understand the pieces:
$url = $_GET['url'];
var_dump($url);
$url = rtrim($url, '/');
var_dump($url);
$url = filter_var($url, FILTER_SANITIZE_URL);
var_dump($url);
$url = explode('/', $url);
var_dump($url);
这篇关于使用PHP和.htaccess进行url路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!