Pretty Urls in PHP Frameworks

Pretty URLs in PHP frameworks

This is usually done by routing all requests to a single entry point (a file that executes different code based on the request) with a rule like:

# Redirect everything that doesn't match a directory or file to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]

This file then compares the request ($_SERVER["REQUEST_URI"]) against a list of routes - a mapping of a pattern matching the request to a controller action (in MVC applications) or another path of execution. Frameworks often include a route that can infer the controller and action from the request itself, as a backup route.

A small, simplified example:

<?php

// Define a couple of simple actions
class Home {
public function GET() { return 'Homepage'; }
}

class About {
public function GET() { return 'About page'; }
}

// Mapping of request pattern (URL) to action classes (above)
$routes = array(
'/' => 'Home',
'/about' => 'About'
);

// Match the request to a route (find the first matching URL in routes)
$request = '/' . trim($_SERVER['REQUEST_URI'], '/');
$route = null;
foreach ($routes as $pattern => $class) {
if ($pattern == $request) {
$route = $class;
break;
}
}

// If no route matched, or class for route not found (404)
if (is_null($route) || !class_exists($route)) {
header('HTTP/1.1 404 Not Found');
echo 'Page not found';
exit(1);
}

// If method not found in action class, send a 405 (e.g. Home::POST())
if (!method_exists($route, $_SERVER["REQUEST_METHOD"])) {
header('HTTP/1.1 405 Method not allowed');
echo 'Method not allowed';
exit(1);
}

// Otherwise, return the result of the action
$action = new $route;
$result = call_user_func(array($action, $_SERVER["REQUEST_METHOD"]));
echo $result;

Combined with the first configuration, this is a simple script that will allow you to use URLs like domain.com/about. Hope this helps you make sense of what's going on here.

How to create pretty URL's with a custom MVC Framework in PHP?

Assuming you are not using an existing MVC framework that already gives you this, and that you want to create your own, my suggestion would be for you to implement a Front Controller. Here is a tutorial on how to do it: http://onlamp.com/pub/a/php/2004/07/08/front_controller.html

You can also take a look at an extremly simple example here: What is a Front Controller and how is it implemented in PHP?

Site URLs using a PHP pretty url framework

The easy alternative would be to use <base href="http://example.org/your/project/index" /> in your page templates <head>. But that's basically like having full URLs generated. And yes, it's also valid for XHTML and still in HTML5.

How do I use pretty URLs in Yii without a root path?

It was because of Apache. Without pointing to index.php, Apache didn't know where to send the request and is the one giving the error. This will default to index.php.

# For pretty URLs
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

https://komarashettynageshrao.wordpress.com/2010/02/04/pretty-urls-in-yii/

Enabling pretty URLs / urlManager completely breaks Yii 2 application runnikg as a REST micro-framework

This question comes from an misunderstanding. I wasn't aware that when pretty URLs are enabled then yii\rest\UrlRule::$pluralize is set to true by default. Meaning that the request must be:

http://localhost/micro-app/posts

(plural controller name; notice "s" at the end)

Not:

http://localhost/micro-app/post

(singular controller name)

It is also completely normal that when pretty URLs are enabled then regular URLs doesn't work anymore. That's the reason for getting 404 File Not Found on http://localhost/micro-app/index.php?r=post URL when pretty URLs are enabled.

set & read pretty URL

You need to use Apache's mod_rewrite.

You will need to add the following lines in your .htaccess file:

RewriteEngine On
RewriteRule ^[a-zA-Z0-9]/?$ users.php?username=$1 [NC,L]

If you want to read more about it, here is a nice article about URL Rewriting for Beginners.



Related Topics



Leave a reply



Submit