Monday, August 29, 2011

Swiz URLMapping

Swiz Framework, since the 1.0 version, has a great feature that allows you to create your own metadata processor. I didn't create any of my own yet but I found a great one created by Ryan Campbell - check his site here LINK. Generally speaking it listens to any URL change in your browser and for example can call a method if the URL is mathing the metadata argument. It has some other nice features (like changing the title of the page), but I will only show you the simplest, but yet I think very useful example - invoking a login screen. Let's look at this code:

[URLMapping(url = "/signin")]
public function signIn():void
{
  // put here some code that will invoke sign in window
}

It's so simple and trivial that I don't need to explain this anymore (I heard that all the time during lectures on my physics studies - of course it was trivial only for the professor). OK of course I'm kidding, but enough of this stupid jokes. Thanks to this code, when you write "/signin" at the end of your URL then function signIn() will be called. I used that on my last project and it works great. When you want to log in as an admin you just use this URL to start the application. You don't need to put any login button into UI, and standard user doesn't even need to know how to log in to the application (in this project you need to log in only if you want ot do some admin business). Last but not least - remember that function decorated by this metadata tag needs to be public!

No comments:

Post a Comment