Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do MVC form url formatting?

I am using PHP. I want to create an MVC setup from scratch to learn more about how MVC works. I want to use clean urls with slashes as delimiters for the arguments. How do people do this when it comes to GET method forms? Or do people avoid GET method forms all together?

As of right now the ways I can imagine are:

  1. Don't use GET method forms (although this makes it harder to let users bookmark/link in some cases).
  2. Use AJAX instead of form submission (although what do you do for SEO and JS disablers?).
  3. Have page submit to itself with post method, then reform the post vars into an url, then rerout to that url using headers (seems like wasted resources).

Any suggestions or suggested reading welcome.

like image 837
dqhendricks Avatar asked Dec 03 '22 03:12

dqhendricks


1 Answers

You can use a .htaccess file like this

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

So... if the url is

http://example.com/controller/action/param1/

you can path the controller and the action, the index.php receive the var url [string]
and you can split them to load the controller...like

$params = explode('/', $_GET['url']);
$controller = new $params[0];//load the controller
$controller->$params[1]($params[2]);//ejecute the method, and pass the param
like image 193
Alejo JM Avatar answered Dec 16 '22 19:12

Alejo JM