Serverside Webscripting [JLW384]

02.url.design

It looks like you are viewing these slides on GitHub. Please clone the source repository in order to get the included PHP examples to work.

URL Design

Not enough room? My place is 2 cubic meters and we only take up 1.5 cubic meters. We've got room for a whole nother 2/3rds of a person.

URLs in the olden days

  • In the beginning
    • http://example.org/index.php?action=zomg&page=lol
  • Then SEO and “nice urls” came along
    • http://example.org/zomg/lol
    • http://example.org/videos/latest/hamburgers
    • http://example.org/search/lolcats/pictures/yes/1/200

Richardson Maturity Model

  • Model describing the principal elements of a RESTful API
    • Level 0: Use HTTP as transport system
    • Level 1: Identify and organize resources in a hierarchy
    • Level 2: Use HTTP verbs for operations
    • Level 3: HATEOAS
  • Level 1 is what we'll be focussing on

Identification & Hierarchy

  • Basic naming conventions
    • Verbs are bad, nouns are good
    • Use plurals
    • Respect the Hierarchy
  • Examples
    • /product/products
    • /product/1234/products/1234
    • /photos/product/1234/products/1234/photos
    • /photos/product/1234/5678/products/1234/photos/5678
This doesn't apply for “normal pages” of your site

Identification & Hierarchy bis

  • Use concrete naming, yet don't be too specific
    • /things
    • /animals
    • /dogs
    • /beagles

Example

  • /
  • /movies
  • /movies/{id}
  • /movies/{id}/photos
  • /actors
  • /actors/{id}
  • /actors/{id}/movies
  • /about
  • /contact

Supporting Nice URLs

I installed shock absorbing bumpers on your ass to minimize the chance of catastrofic butt failure.

Supporting nice URLs

  1. Let all requests to inexistent files/folders be processed by index.php
  2. Build index.php in such a way so that it loads the requested content based upon the URL

1. Howto: Apache

  • Possible using the mod_rewrite Apache Module
    • Wampserver: not enabled by default (instructions)
    • MAMP Pro: enabled by default
  • Put a file named .htaccess in your root to rewrite requests to non-existing files to index.php
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
    </IfModule>

1. Howto: IIS

  • Possible using the IIS Rewrite Module
  • Put a file named web.config in your root to redirect requests to non-existing files to index.php
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    	<system.webServer>
    		<rewrite>
    			<rules>
    				<rule name="Main Rule" stopProcessing="true">
    					<match url=".*" />
    					<conditions logicalGrouping="MatchAll">
    						<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    						<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    					</conditions>
    					<action type="Rewrite" url="index.php" />
    				</rule>
    			</rules>
    		</rewrite>
    	</system.webServer>
    </configuration>

1. Howto: Built-in PHP 5.4 server

  • All requests route to index.php by default
    • You'll need to add some extra PHP code to index.php to force non-404 requests route to the correct file
    <?php
    
    $filename = __DIR__ . preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']);
    if (php_sapi_name() === 'cli-server' && is_file($filename)) {
    	return false;
    }
    
    // rest of your code here
Note: You can start a server by running php -S localhost:8080 in your project www-root

2. Howto

  • Write something yourself.
    • Use the value in $_SERVER['REQUEST_URI'] to get started
  • Install a router library that does this for you.

2. Example

<?php

require __DIR__ . '/vendor/autoload.php';

$router = new \Bramus\Router\Router();

$router->get('/', function() {
	echo 'index';
});

$router->get('/hello', function() {
	echo 'hello';
});

$router->get('/hello/(\w+)', function($name) {
	echo 'hello ' . htmlentities($name);
});

$router->run();

Questions?

Sources

ikdoeict.be