Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic MVC structure, URL minimization and GET

Tags:

php

.htaccess

get

I'm building a simple MVC structure, my intention is to also have simple URLs (without the file and .php extension), basic rules:

  • Everything goes through index.php
  • /index.php/1/2 would be a typical URL, 1-loads the controller class (if such controller exists), 2-calls method (if such method exists)

Here is how I'm hiding index.php from my URLs on the htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

This allows me to do /1/2

To determine my URL in PHP, I do:

$url_parts = array_filter(explode("/", $_SERVER["REQUEST_URI"]));

This gives me access to all ny URL segments, I load different files depending on $url_parts[0] (controller) and $url_parts[1] (method/function)

I only have one concern now, and that is working with $_GET on my pages, if I access something like:

/1/2?foo=bar

My $_GET returns:

Array ( [/1/2] => )

Whereas I'd obviously want it to be:

Array ( [foo] => bar )

Can my code be salvaged at all? I obviously don't know how $_GET works that well, I was expecting this to work OK.

like image 823
Jorg Ancrath Avatar asked Nov 19 '25 20:11

Jorg Ancrath


1 Answers

Look at the QSA (Query String Append) flag, e.g.

RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]
like image 141
CompuChip Avatar answered Nov 21 '25 10:11

CompuChip