Jump to content


[PHP] URLs and MVC


2 replies to this topic

#1 Killswitch

    Young Padawan

  • Members
  • Pip
  • 20 posts

Posted 07 November 2009 - 09:24 PM

Hey guys, having a bit of a time. I am learning to work with MVC. I have everything working fine, but I would like to rewrite the urls a little more friendly than what they are. For example...

site.com/control/view/id/5 => site.com/control/the-page

I really am not sure how to go about this, since the view/id is relied upon to determine which method is being called. Since this could vary, I'm not sure if I used mod_rewrite to remove this if it would be able to determine everything correctly or not.

Does anyone have any experience in this field that could help point me in the right direction?

#2 NGPixel

    Senior Programmer

  • P2L Staff
  • PipPipPipPip
  • 1,410 posts
  • Gender:Male
  • Location:Montreal, Canada
  • Interests:Web Design : Coding : Animation

Posted 07 November 2009 - 10:02 PM

I would suggest you look at frameworks like CodeIgniter which does exactly that.

http://codeigniter.com/

Then you don't have to worry about the linking. The right controller and method will be called depending on the url (along with the parameters).

If you wish to use your own code, then you will need to either create routes in your code to call the right method from the URL or use mod_rewrite with defined rules.

#3 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 11 November 2009 - 03:24 PM

I would suggest looking into CodeIgniter or something similar like NGPixel stated but if you want to build it yourself...

I was curious myself once and took the wordpress htaccess and echo'd the $_GET and $_SERVER arrays to get an idea how the information was being passed to index.php.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users