使用 PHP 和 .htaccess 进行 url 路由 [英] url routing with PHP and .htaccess

查看:29
本文介绍了使用 PHP 和 .htaccess 进行 url 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 MVC 模式将我的逻辑与演示文稿和数据分开.

I want to use the MVC pattern to divide my logic, from the presentation and the data.

嗯,我一直在寻找 i.但事实是,我什至不知道要搜索什么.

Well, i've been searching for i while. But the truth is that i don't even know what to search.

我正在尝试在 php 中设置一个 MVC 框架.我正在关注 youtube 上的教程,但我被困在路由点.

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.

重点是:

我有一个 .htaccess 文件,其中包含一些指令(但问题是我不知道所有这些指令的含义.我从来没有理解 htaccess 逻辑)

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]

然后我有这些 php 脚本:

And then i have these php scripts:

<?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_varrtrim.因为这正是我想弄清楚的.正如我之前所说,这段代码不是我的.我确信这个技巧就在 .htacess 和 App.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';
    }
}

如果我通过这个网址:localhost/~caiuscitiriga/mvc/public/home/index/maxine
我明白了: Array ( [0] => home [1] => index [2] => maxine )

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]

我读了上面的内容,如果请求不是目录,也不是文件,则获取路径并将其传递给内部 index.php 并使用 url 属性作为路径.

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.

那么现在

//example.com/big/bad/mamma

映射到

 //example.com/index.php?url=big/bad/mamma

如果你愿意,你可以调用上面的脚本.

You could call the script as above if you want.

然后您的解析 url 将获取 url ('big/bad/mamma') 的值,如果有斜杠,则删除尾部斜杠.然后在遇到正斜杠的地方拆分字符串.所以你最终得到了三个部分.这就是您的阵列中所拥有的.

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.

来自手册:

FILTER_SANITIZE_URL 过滤器将删除除字母、数字和 $-_.+!*'(),{}|\^~[]`<>#%";/?:@&= 之外的所有字符.

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 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆