Eli
Eli

Reputation: 4359

how to make codeigniter style URI segments

I was wondering how one could make codeigniter style url segments on a project.

Aside from an htaccess rule to capture the segments, how can this be done in PHP?

After snooping around the codeigniter source code, i could not find any reference to an htaccess file that captures the usage.

Any idea on how this can be done?

Upvotes: 1

Views: 832

Answers (4)

landons
landons

Reputation: 9547

You still need something to "forward" all virtual requests to a physical file.

The idea is that any URI that doesn't match a physical file or folder on disk is rewritten (usually through mod_rewrite) to your index.php file (it's usually your index file so a direct call to the index works too), and it appends the URI to the path or as a query string parameter:

RewriteCond %{REQUEST_FILENAME} !-f  # Not a real file
RewriteCond %{REQUEST_FILENAME} !-d  # Not a real folder
RewriteRule ^(.*)$ index.php/$1 [L]  # Rewrite to index.php, with a leading slash, followed by the URI

Alternatively, you can use a standard error document handler (still in .htaccess or apache config, but no need for mod_rewrite!):

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

Now that control is passed to uniformly to an index.php file, you need a router mechanism to match the route to the correct controller. Ideally, you'd have a list of static and dynamic routes. Static routes are direct matches to the URI, and dynamic would be regular expression matches. Check your static routes first, as it's as simple as a single hash lookup, while you'll have to loop through all the dynamic routes.

For performance, it's nice to put your more common dynamic routes at the beginning of the list, and the obscure ones at the end.

Upvotes: 1

Xeoncross
Xeoncross

Reputation: 57244

Assuming you are passing ALL requests to index.php via Apache mod_rewrite or Nginx:

// Get the current URL path (only) and convert encoded characters back to unicode
$path = rawurldecode(trim(parse_url(getenv('REQUEST_URI'), PHP_URL_PATH), '/')));

// array merge with defaults
$segments = explode('/', $path) + array('home', 'index');

//First two segments are controller/method
$controller = array_shift($segments);
$method = array_shift($segments);

// @todo serious security checking here!

// Finally, load and call
$controller = new $controller;
$controller->$method($segments);

Your controller would look like this

class Home
{
    public function index($params)
    {
        print_r($params);
    }
}

Upvotes: 2

d2burke
d2burke

Reputation: 4111

Using the .htaccess file to employ mod_rewrite to rewrite the base of the url is essential to this. Otherwise, the browser will treat each segment as a supposed folder or file (depending on whether or not you have a trailing /)

once you have that, however, you can simply use the method described in this post:

how do i parse url php

Upvotes: 0

Kristian
Kristian

Reputation: 21830

What you do is set up one single url param in htaccess, and then use a string splitting method to retrieve a model, controller, and view from that string which will then call a model class, a controller class, and then render the data into a view. the transformation would be something like the following:

mysite.com/index.php?url=/tasks/all => mysite.com/tasks/all which calls the task model, which then calls the function called "all()" inside of the tasks controller.

As soon as this site goes back online, do look at the htaccess portion of the tutorial -- he did a good job of showing how its done http://www.henriquebarroso.com/how-to-create-a-simple-mvc-framework-in-php/

Upvotes: 1

Related Questions