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:
- Don't use GET method forms (although this makes it harder to let users bookmark/link in some cases).
- Use AJAX instead of form submission (although what do you do for SEO and JS disablers?).
- 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.
You can use a
.htaccess
file like thisSo... 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
If you are interested in understanding how MVC frameworks / principles work sometimes the best way is first to understand how to implement them (something like CodeIgniter) you can then delve into the source code / documentation and understand how it works.
If you want to jump in at the deep end look into reading up on MOD_REWRITE to solve your URL concerns. GET is usable - you will just rewrite the URL components.
Cfreak the best way to answer this question would be for you digg into the various PHP frameworks source code to see how they've implemented an MVC approach. The PHP frameworks I've used typically exclude GET data and rely soly on POST data. However information can be passed via a query string see example below.
Typically URL's in PHP MVC frameworks look something like this: example.com/index.php/controller/action/arguments. The index.php part is usually removed by using an .htaccess file. You controller is a class, that most likely inherits from some sort of parent controller class. You controller has actions/methods whatever you prefer that are called next. You can pass these methods arguments, typically an unlimitied number of arguments by tacking them onto your URL. I hope that helps to give you a basic idea of the structure. Everything starts in index.php, which will include all your necessary config files, and load and required classes you may need :)
The question you are basically asking is how to achieve SEO-friendly(Search Engine Optimization Friendly) URLs, in the format "www.site.com/controller/action/params". This is achieved through editing your .htaccess file in a few key folders to redirect server to specified folders to gain a common access point for your web application, using MOD_REWRITE. I would take a look at this tutorial. The first few paragraphs explain how to achieve the URLs you are looking for.