If Only I Knew That!

Webby things that could save you time

Codeigniter URI routing to give impression of directories

with one comment

I recently had to create a codeigniter site with three main sections and two of the sections needed to appear in directories in the URL’s

  • The main site
  • Backend user dashboard
  • Backend user admin


All the main site pages were available via top level URL’s, nothing new there with the standard codeigniter routing


http://example.com/about

http://example.com/join

these simply route to

/application/controller/about.php
/application/controller/join.php

The backend dashboard needed to appear to be in a ‘dashboard’ directory like so


http://example.com/dashboard/events

these route to

/application/controller/events.php

and finally there was a admin section within the dashboard section and the URL’s looked like so


http://example.com/dashboard/admin/events/add

http://example.com/dashboard/admin/events/delete

these routed to

/application/controller/admin_events.php

the admin_ was required to prevent the events controller conflicting but I didn’t want admin_events
to be part of the URL.

So to allow the directory based URL’s and remove the requirement to show the admin_ in the URL to call the right controller, a little work in the /application/config/routes.php was required.

$route['^dashboard/admin/(.+)$'] = "admin_$1";
$route['^dashboard/(.+)$'] = "$1";

Written by daveganley

April 14th, 2009 at 10:49 am

Posted in Codeigniter

Tagged with ,

One Response to 'Codeigniter URI routing to give impression of directories'

Subscribe to comments with RSS or TrackBack to 'Codeigniter URI routing to give impression of directories'.

  1. You my friend are awesome. I never once thought of using routes to create the appearance of an admin back-end. I’ve been looking up countless ways of doing it, routes seems to be the easiest if front-end/back-end separation isn’t required.

    Dwayne

    2 Jun 10 at 1:28 pm

Leave a Reply

Powered by CDN Rewrites