Clean URLs

Login or register to post comments
6 replies [Last post]
Offline
Joined: 01/29/2009

I know that this is kind of cliche topic but what the hell!

Some time ago I've tried django framework (for python). It is cool, but I couldn't make the switch (think I am too far into qcodo). The most impressive part for me were the the clean urls. I've copied the functionality to Qcodo.

First of all, some facts:
- I use one index.php as a dispatcher for QPanels (each page is different QPanel).
- The directory structure follows MVC (I have the QPanels in controllers directory, templates in views etc) - I've copied it from a tutorial that was published some time ago.

The URLS are defined like this:

Urls[] = url('user/register$',"UserRegister","user/register.class.php","user.register");
Urls[] = url('user/{integer}[id]$',"UserProfile","user/profile.class.php","user.profile");
Urls[] = url('article/{slug}[slug]$',"ArticleView","article/view.class.php","article.view");

Parameters:

  1. Pseudo regex pattern, {} - validation rule, [] - name of parameter. Parameters are stored in array and can be referenced later in QPanel. Available validation rules are (alpha),(digit),(nodigit),(slug) - it is rather simple to define your own
  2. Class name inside a file from 3rd parameter
  3. Path to class
  4. Reference name

And now the best part! URLS are completely decoupled from the Views. If I want to reference the URL for a user (id 123) profile I use my "URL reversal" function:

_r("user.profile",123)

If the number of parameters is three (thank you for eval() function ;) ) I can write:

_r("view.name",1,2,3)

If I want to change URL I do it in one place (I can even localize URLS :) ) - following DRY for URLS also.

The solution consists of 2 classes that need to be polished (I am not a professional developer). It is rather quick and dirty but it works in my current project. If you want I can somehow publish the whole tutorial.

alex94040's picture
Offline
Joined: 11/06/2008

This is very interesting! In fact, I'd encourage you to create more than just a tutorial - I'd encourage you to create a plugin that has a series of examples and all the relevant files.

Would you be up for it?

Offline
Joined: 01/29/2009

Yes I can try :) First of all, I have to read about plug-in architecture.

Do you think that such method of rewriting URLs is suitable for a plug-in? What I mean is that the changes are rather deep. These classes have to be called each time page is loaded to check which QPanel matches the URL. There is also a change in .htaccess file.

alex94040's picture
Offline
Joined: 11/06/2008

I wouldn't integrate it so deeply - I'd instead write a plugin where this kind of a structure is completely implemented for a sample directory. That'd make it easily installable / it'd explain things well.

OOPMan's picture
Offline
Joined: 11/07/2008

Hi guys

This looks a lot like the Django-style Front-end controller system I developed for a QCodo application I'm working on.

The system I developed for our application is similar, but different in some regards.

It is, however, very flexible and can even generate URLs using input parameters.

It also has a number of important speed optimisations that are necessary when using regex for URL handling.

I will check with my bosses and see whether or not I can provide the source if anyone is interested...

alex94040's picture
Offline
Joined: 11/06/2008

OOPMan - this is great, it would be awesome if you could share it in some way (ideally, as a plugin :-))

OOPMan's picture
Offline
Joined: 11/07/2008

I will check with my bosses and see if I can share the code. I'm not sure how easy it would be to wrap it up as a plugin.

Also, I think it could do with some modifications for general purpose use.

UPDATE: I have received permission to release a modified version of the system. I will wrap it up as a plugin and try to get something out late next week.