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??