tags:

views:

300

answers:

3

Hi Guys,

I'm having a really hard time getting my head around using REST-style URLS. Can I get a little help with this? Right now I have a query string like so:

example.com/calendar_expanded?date=1270094400

I have a mod rewrite that's hiding the .php extension. How do I

  1. Make this calendar_expanded/date/1270094400 happen.
  2. $_GET the values from the url after the fact?

I love the concept behind it but working 16-hour days to meet a deadline for an entire month is taking a toll on my brain.

Thanks for the help.

+1  A: 

You need to inspect $_SERVER[ 'REQUEST_URI' ] or $_SERVER[ 'PATH_INFO' ], not $_GET.

$_SERVER[ 'REQUEST_URI' ] will hold this /calendar_expanded/date/1270094400?quertStringParams=1&etc...
$_SERVER[ 'PATH_INFO' ] will hold this /calendar_expanded/date/1270094400

In other words, you will have to break down these values into their seperate segments, by using explode() and the likes. I suggest you use $_SERVER[ 'PATH_INFO' ] as a subject, since the real query string variables will be available in $_GET already.

fireeyedboy
+3  A: 

If you want to create quite a lot REST URLs you should consider to use a PHP framework that makes use of the Front Controller design pattern.

This way, every request is processed by the framework and you are flexible to design your URLs however they should look like.

E.g. Symfony supports a RESTful design out of the box.

BTW RESTful URLs only make sense if the parameters are somehow fixed and specify a resource, for example the title of a blog post. If you want to pass a parameter only to e.g. control the sorting of a table (i.e. the view of something) then this should go as a "normal" GET parameter into the query string.

In your case it looks like you want to control what day should be shown in a calendar. I personally would just use the normal GET query string here.

Felix Kling
As does Kohana, a framework we use at my job. It defaults to routing a url like this example.com/my_controller/controller_method/param1/param2 to a controller called to a method called "controller_method" inside a controller called "my_controlled" with paramaters "param1" and "param2"... pretty neato.
sberry2A
I think this is the most correct answer and Felix you looked at what I actually wanted to do. You're right, I'm passing date variables around to control a calendar. I guess I'll stick with my existing format. I'm going to read up on this though because I feel like it might come in handy at some point. Thanks.
jeerose
+1 for making the distinction between REST and just nice looking URLs. I've seen some confusion where people think that by using URL rewriting, they're making their app RESTful - which isn't always the case.
Bryan M.
+2  A: 

Is this what you're looking for?

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

This is what I remember CakePHP doing when it first came out... dunno if it still does...

LeguRi