PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Aug 18, 2018 4:11 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: URL routing challenge
PostPosted: Sat Apr 24, 2010 12:11 am 
Offline
Forum Commoner

Joined: Tue Mar 30, 2010 6:18 pm
Posts: 61
Quote:
Implement regex-based URL routing: Given a URL, passed by the web server to your front controller, find the most efficient way to match said arbitrary URL against an arbitrarily large list of URL regex's, with an emphasis on avoiding the overhead of repeated preg_match (or similar) calls.

Expanded requirements:

1. Provide a central system to control URL routing. This may involve classes or objects registering themselves with the routing system, invoking a method to add URL patterns to the router, or something as simple as adding specifically defined URL's that the server can access.

2. Gracefully handle 404 errors (that is, do not rely on web server 404 errors as part of routing; this destroys analytics). Pass a 404 to the server when appropriate, i.e. your router has been given a URL that does not match any pattern provided by the application.

3. Pass your views to the router. Use PHP's various methods of invoking methods or functions to allow your URL routing to establish either functions or class methods associated with particular URL's, including slugs or ID's provided by the views.

This exercise is inspired by the SEO-friendly URL's that Django and Rails make simple. The concept really isn't that difficult; the regex matching in this exercise is how this routing is generally handled. Learning its particulars can give you a great deal of insight into both native SEO (generating slugs from object titles is an incrediby efficient and simple way to start great search engine optimization) and provide readable URL's for your clients.

A test of the feasibility of solutions to this problem: Ability to specify arbitrary URL's including named patterns (see preg_search documentation) and relay these patterns to methods or functions.

This challenge does not necessarily involve defining templates for the provided callbacks, though that is the logical progression of the exercise. Simply displaying output indicating that the correct method/function has been called is acceptable.

It's not easy: This is something that's a major feature in most frameworks. I suggest it because it's very educational. It took me several hours to implement; however, I knew in advance what I was doing and how to do it, and I have a great deal of PHP experience. I would expect reasonably seasoned PHP developers to produce useful solutions within a few days.


Last edited by phu on Sat Apr 24, 2010 12:22 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Sat Apr 24, 2010 12:21 am 
Offline
Forum Commoner

Joined: Tue Mar 30, 2010 6:18 pm
Posts: 61
Examples:

url = array('^$', 'index');
// Invokes function 'index' with no arguments
url = array('^test/(.*)', 'testfunction');
// Invokes testfunction with one argument, being everything after the slash in the URL
url = array('^([\w-]+)/(\d)', array('cls', 'mtd'));
// Invokes method mtd on class cls with two arguments, one slug and one numeric

The assignment is completely arbitrary here; it's just meant to suggest how defining URL's should work and how input should be handled. The handler itself should fall back on a 404 in the event of a URL not matching any provided pattern.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group