apache - MVC url routing with PHP and .htaccess -


i'm new mvc pattern approach. i've started developing in php year ago, , want improve knowledge , projects too. want use mvc pattern divide logic, presentation , data.

well, i've been searching while. truth don't know search.

i'm trying setup mvc framework in php. i'm following tutorial on youtube, , i'm stuck @ routing point.

i've read lot of guides, , every single 1 teaches things in different ways, creating more confusion.

the point this:

i have .htaccess file contains directives (but problem don't know directives means. i've never understood htaccess logic)

options -multiviews rewriteengine on  #i think sets base url of site? rewritebase /~caiuscitiriga/mvc/public  #what mean?? rewritecond %{request_filename} !-d rewritecond %{request_filename} !-f  #and this?! rewriterule ^(.+)$ index.php?url=$1 [qsa,l] 

and 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 used filter_var , rtrim. because want figure out. said before, code isn't mine. i'm sure trick it's in .htacess , app.php don't understand 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 pass url: localhost/~caiuscitiriga/mvc/public/home/index/maxine
this: array ( [0] => home [1] => index [2] => maxine )

why?!!? mean, it's correct. why??

rewritecond %{request_filename} !-d rewritecond %{request_filename} !-f rewriterule ^(.+)$ index.php?url=$1 [qsa,l] 

i read above as, if request not directory, , not file, take path , pass index.php internally url attribute path.

so now

//example.com/big/bad/mamma 

maps to

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

you call script above if want.

then parse url taking value of url ('big/bad/mamma'), removing trailing slash if there one. , splitting string wherever encounters forward slash. end 3 parts. have in array.

from manual:

the filter_sanitize_url filter remove characters except letters, digits , $-_.+!*'(),{}|\^~[]`<>#%";/?:@&=.

but break down if want understand 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); 

Comments