Some really basic questions to do with RESTful and PHP - php

I am working on a first attempt at a RESTful API. I have designed some simple classes to represent each element of my system including a Message and Room class (it's a chat application with any amount of rooms containing any amount of messages).
I constantly see URIs in the form of, for example
/room (this would get all rooms)
/room/{room_id} (this would create, update or delete a room with this ID)
/room/{room_id}/message (this would get all messages from a room with the specified ID)
/room/{room_id}/meesage/{message_id} (this would create, update or delete a message with this id)
So I guess my two questions are:
1) How do I redirect these requested URIs to my single handling script (located in /api/api.1.0.0.php)
2) How do I request these URIs with the correct HTTP verb like put, delete, update etc.
EDIT: I should add that I am looking to do this without a framework.

Here is how to use mod_rewrite on apache to redirect all requests to a single script. You will need to use apache and have mod_rewrite installed/setup (it is installed on most shared hosting and is easy to add to a self managed system).
Create a .htaccess file in the sub-folder where the API exists under the document root. Add the following to the file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?URI=$1 [L,QSA]
The first condition will make sure the file doesn't exist so you can still load files that do exist (without it, requests for even valid files will rewrite). The second condition does the same but checks that it isn't a valid directory that is being requested. The last line, the rule, captures the URI and tells apache to load index.php instead passing the URI to the URI get parameter. At the end of the rule are flags, L and QSA. The L flag tells apache to stop processing rules if this one is satisfied and the QSA tells apache to automatically append any query string (get string) parameters that were passed.
You can then access the URI in php using $_GET['URI']. A simple $URI = explode('/', $_GET['URI']); will get you a nicely formatted array of each parameter passed.

As for directing to correct part of the app after requesting particular url you should read about routers. All of the major MVC frameworks will have advanced routing solutions. http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html
If you want to go without framework and use single scripts Apache mod_rewrite is your friend.
For creating requests using different http methods use any of the REST clients like Postman for Chrome.
EDIT:
.htaccess example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [NC,L]

Related

Can Symfony 4 be configured to ignore code installed in subdirectories?

I'm currently converting an old website to use Symfony 4 and the site uses the LiveZilla live chat app.
LiveZilla lives in a subfolder of the site, and is accessed directly using URLs under somesite.com/livezilla. I can't currently access it of course, because I haven't configured a route for this folder. So Symfony decides that this will be a 404.
LiveZilla is essentially a completely separate app living in its own folder here. It isn't dependent on the website hosting it. Is there a way to tell Symfony to ignore certain paths so that code like this can be executed without interference?
I have a sneaking feeling that I need to adjust the way I am looking at this as I can't find anything obvious in the Symfony docs about this, and the framework is pretty well thought out. The best I have come up with so far is hacking public/.htaccess, but it feels wrong somehow...
Your .htaccess file should allow requests directly to existing files, but not directories. See this rule:
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
This means you should be able to access somesite.com/livezilla/index.php but a request to somesite.com/livezilla will redirect to the symfony front controller. So try changing your links to point to actual files within the sub-directory.
There is also nothing wrong with editing the .htaccess file to suit your needs. You just need a condition that checks if the request is to the sub-directory and if so use the same RewriteRule ^ - [L] as above to allow that request to continue.
The following should work if placed after the above rule (reference):
RewriteCond %{REQUEST_URI} ^/livezilla/
RewriteRule ^ - [L]
Or this may be better, place this rule immediately after the line RewriteEngine On(reference)
RewriteRule ^(livezilla) - [L]
The [L] flag means the rule will be the last one used for the request.

Get page id value and display in URL via .htaccess

I'm trying to display SEO friendly URLs by using a rewrite in our .htaccess file, but I can't get it to work (I've researched many of the related topics on StackExhange and elsewhere, but to no avail). I'd like to get the value of the id on this page...
http://199.119.123.135/info/tool_surety_company.php?id=1
...and display the id value in the URL instead of the ugly "tool_surety_company.php?id=1".
I'm going for a result like this: http://199.119.123.135/info/travelers-group
I'm using the following code in my .htaccess file:
RewriteCond %{THE_REQUEST} \ /+info/tool_surety_company\.php\?id=([^&]+)
RewriteRule ^ /info/%1/? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^^info/([^/]+)/?$ /info/tool_surety_company.php?id=$1 [QSA]
But I'm receiving a 404 error.
Any ideas? Thanks in advance!
There might be something I'm misunderstanding here but I believe there would need to be a mechanism on the server side code to load the correct content for the new "seo-friendly url". In other words, sure, you can redirect the user to show a different url but how is the server going to know what content to load for that new url?
Here's a good resource for putting together a simple example.
https://moz.com/ugc/using-mod-rewrite-to-convert-dynamic-urls-to-seo-friendly-urls
Update:
From here - https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules
TROUBLESHOOTING
404 Not Found
Examine the new URL in your browser closely. Does it match a file that
exists on the server in the new location specified by the rewrite
rule? You may have to make your rewrite rule more broad (you may be
able to remove the $1 from the second string). This will direct
rewrites to the main index page given in the second string. Or, you
may need to copy files from your old location to the new location.
In other words, the only reason you would be getting a 404 is because the server does not find the file that is requested as defined in the URL visible in your browser address bar.
Htaccess Rewrites are enabled by using the Apache module mod_rewrite,
which is one of the most powerful Apache modules and features
availale. Htaccess Rewrites through mod_rewrite provide the special
ability to Rewrite requests internally as well as Redirect request
externally.
When the url in your browser's location bar stays the same for a
request it is an internal rewrite, when the url changes an external
redirection is taking place. This is one of the first, and one of the
biggest mental-blocks people have when learning about mod_rewrite.
More info from here:
http://www.askapache.com/htaccess/modrewrite-tips-tricks.html

How to have a PHP file be used for multiple URLs

I want to have a single PHP file that takes care of multiple URLs in a subdirectory.
For example, my site is http://www.startingtofeelit.com/. I want one php file, say, called playlist.php which would handle when a user goes to http://www.startingtofeelit.com/playlist/101 or if they go to http://www.startingtofeelit.com/playlist/142 etc. I want to be able to strip the number (101, 142 in my example urls above) to use as a variable (the playlist ID), so I can display the correct playlist.
I know that I can create an index.php in my playlist subdirectory and use GET variables like http://www.startingtofeelit.com/playlist?id=102 and get the ID that way, but this is much sloppier looking and I'd like to be able to know how to do it the other way.
My site is built on WordPress, but I don't think this should make a difference in any way.
Well, you cannot achieve this with PHP alone.
If you use Apache, you can use .htaccess
If you use IIS, you can use URL Rewrite
The basic idea behind those modules is to mapping from one URL to another URL. For example: you would want to map from
http://www.startingtofeelit.com/playlist/142 =>
http://www.startingtofeelit.com/playlist.php?id=142
You can express the URL mapping in regular expression. For example, in .htaccess (Apache). You can write like this
RewriteRule ^playlist/([0-9]+)/?$ playlist.php?id=$1
Noted that, you need to have .htaccess file in your website directory. Since, you are using Wordpress, chance that you have existed .htaccess is high. You can simply append that line of code to existed .htaccess
The following is an explanation of the regular expression:
^playlist/ # any URL start with playlist/
([0+9]+) # following by number, and store it as $1
/?$ # end with or without /
Mapping to
playlist.php?id=$1 # where $1 is taken from the matched number from our pattern.
This is usually handled in a way similar to what you already tried. However, it's common to use a re-writing script so that your application will accept a clean URL such as:
http://www.startingtofeelit.com/playlist/142
...and re-write it for your application as such:
http://www.startingtofeelit.com/playlist?id=142
For example, if you're using an Apache web server and have the mod_rewrite module installed and enabled, you can use the following snippet in an .htaccess file and use your GET parameter as you indicated you already know how to do. Other popular web servers have unique URL re-writing modules that will let you do the same.
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite this:
# http://www.example.com/somepage/1
# ...into this:
# http://www.example.com/somepage?id=1
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

How to use directory names as options or parameters?

I don't even know how this method is called, I just know the behavior I want to achieve.
My example for this is Facebook. If you go to facebook.com/[username or id] you get to the profile page, but I can't imagine that they're creating a directory in their root folder and putting a index file in there for every user.
So how's the following behavior accomplished; You go to somepage.com/foo/bar/hello but actually you're requesting somepage.com/foo?bar=hello ?
Is this even possible with Apache and PHP?
I don't even know how this method is called, I just know the behavior I want to achieve.
That thing is called URI/URL and the local part of it is passed to a webserver. The webserver then processes the request.
Is this even possible with Apache and PHP?
Yes. Not even even. This is what a webserver is for. What happens on the server is entirely shielded by the HTTP protocol which knows only the URI/URL specification which does not regulate if and how that needs to match to concrete processes or files on the webserver.
For example with the Apache HTTP Server there is a famous module called Mod_Rewrite that does URL-Rewriting. Often in a fashion that the user with her browser does not take any notice of it.
Example configuration with a PHP file (Apache HTTPD):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
In a PHP script you can obtain the URI/URL by making use of special variables like $_SERVER['REQUEST_URI'] and $_SERVER['QUERY_STRING'].
Commonly this is made with mod rewrite. There you can make a "path" to a variable of a script.
E.g. http://example.com/user/1/edit could be translated with mod rewrite to http://example.com/index.php?function=edit&userid=1
Such a rule would look like this:
RewriteEngine on
RewriteRule ^/user/([0-9]+)/([a-z]+)$ index.php=function=$2&userid=$1 [L]
The first line activated the rewrite module the second line has a regular expression which must match for rewrite the url internally. If you like you can also make that externally with an [R] modifier instad of the [L].
Have a look to the whole documentation to learn more.
The stuff in the breckets are so called flags which are also well documentated.
I hope that helps!

How to understand PHP's URL parsing/routing?

I just inherited a website built in PHP. The main page of www.mysite.com has a href to www.mysite.com/index/35.html somewhere in the page. In the site's root directory and its children there is no document 35.html.
The number 35 is actually an id found in a DB which also holds the html contents of the page.
If I load URL: www.mysite.com/index.php?id=35 the same page loads.
How does PHP know how to automatically convert
/index/35.html
to
/index.php?id=35
EDIT
Based on the answers, I have found a .htaccess file containing rewrite instructions that would explain the functionality.
However, IIS doesn't seem to (or is not configured) know how to use this. (probably because this is an Apache feature?)
So this begs the following question: Is there a way to configure IIS to work with this?
it will be done usign URL Rewriting using .htaccess - should be in the webroot.
It may look something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
May have other bits, but what this basically tells apache is to send anything that DOES NOT physically exist to index.php
It doesn't. There is a mod_rewrite rule that rewrites from /index/foo to /index.php?id=foo, either in a .htaccess file somewhere or in the httpd configuration itself.
RewriteEngine On
RewriteRule ^index/([\d]+)\.html /index.php?id=$1 [NC,L]
This is off the top of my head. Any browsers trying to load an address starting with index/ has any number ending in .html will be internally redirected to index.php?id= whatever the number is.
Edit: Just saw that your working on IIS. This probably won't work for you. Sorry.
I think you will be using .htaccess to redirect all requests to index.php. From there You can pass the query string a routing class, which will parse the url and identify the unique ids.
In this case we can say like, your routing class will parse the request /index/35.html to indexController, indexAction, id=35. now you can pass this id to the model to get corresponding page contents
NB : Here I a am assuming you are using mvc pattern. Anyway it can be treated in your own way, with the concept remaining the same. Hope this make sence.

Categories