问题
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??
回答1:
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);
来源:https://stackoverflow.com/questions/34981045/url-routing-with-php-and-htaccess