Facebook Vanity URLs

8 messages Options
Embed this post
Permalink
mapes911

Facebook Vanity URLs

Reply Threaded More More options
Print post
Permalink
Hi all,

I've been wondering this for a while now about how Twitter/Myspace and now Facebook creates what they are calling "Vanity URLs"

So, their users use their username as their url.

ex. www.mydomain.com/myusername  would route to their user profile.

So my question is, how can you accomplish this with Zend Framework AND still have other controller/action pairs as well?

Is there a way to setup a custom route where you check to see if that first param in the URL is a username, then if not.. attempt to use default routing?

I have been using default routing completely in the site I am building so I am very new to the router.

Thanks in advance for your help!
Matthew Ratzloff

Re: Facebook Vanity URLs

Reply Threaded More More options
Print post
Permalink
$front  = Zend_Controller_Front::getInstance();
$router = $front->getRouter();

$router->removeDefaultRoutes();

$router->addRoute(new Zend_Controller_Router_Route(
    ':username',
    array(
        'module'     => 'user',
        'controller' => 'profile',
        'action'     => 'view'
    )
));

$router->addRoute(new Zend_Controller_Router_Route_Module(
    array(), $front->getDispatcher(), $front->getRequest()
));

That's untested, but I believe it should work.

Hope that helps,

-Matt

On Tue, Jun 9, 2009 at 2:59 PM, mapes911 <[hidden email]> wrote:

Hi all,

I've been wondering this for a while now about how Twitter/Myspace and now
Facebook creates what they are calling "Vanity URLs"

So, their users use their username as their url.

ex. www.mydomain.com/myusername  would route to their user profile.

So my question is, how can you accomplish this with Zend Framework AND still
have other controller/action pairs as well?

Is there a way to setup a custom route where you check to see if that first
param in the URL is a username, then if not.. attempt to use default
routing?

I have been using default routing completely in the site I am building so I
am very new to the router.

Thanks in advance for your help!
--
View this message in context: http://www.nabble.com/Facebook-Vanity-URLs-tp23952083p23952083.html
Sent from the Zend Framework mailing list archive at Nabble.com.


Tim Fountain

Re: Facebook Vanity URLs

Reply Threaded More More options
Print post
Permalink
In reply to this post by mapes911
2009/6/9 mapes911 <[hidden email]>

I've been wondering this for a while now about how Twitter/Myspace and now
Facebook creates what they are calling "Vanity URLs"

So, their users use their username as their url.

ex. www.mydomain.com/myusername  would route to their user profile.

So my question is, how can you accomplish this with Zend Framework AND still
have other controller/action pairs as well?

I would use a custom route class for this (a class extending Zend_Controller_Router_Route). In the match() method you then take the first part of the URL and do a quick database query to see if that matches a username. If it does, set appropriate parameters. If not, return false (and the next route would be checked).

--
Tim Fountain
http://tfountain.co.uk/
Nick M

Re: Facebook Vanity URLs

Reply Threaded More More options
Print post
Permalink
In reply to this post by mapes911
I used the router route hostname and chained it with a standard router:

routes.user.type = "Zend_Controller_Router_Route_Hostname"
routes.user.route = "www.domain.com"
 
routes.user.chains.following.type = "Zend_Controller_Router_Route"
routes.user.chains.following.route = ":username/following/*"
routes.user.chains.following.defaults.controller = view
routes.user.chains.following.defaults.action = following
 
routes.user.chains.followers.type = "Zend_Controller_Router_Route"
routes.user.chains.followers.route = ":username/followers/*"
routes.user.chains.followers.defaults.controller = view
routes.user.chains.followers.defaults.action = followers
 
routes.user.chains.username.type = "Zend_Controller_Router_Route"
routes.user.chains.username.route = ":username/*"
routes.user.chains.username.defaults.controller = view
routes.user.chains.username.defaults.action = index



mapes911 wrote:

> Hi all,
>
> I've been wondering this for a while now about how Twitter/Myspace and now
> Facebook creates what they are calling "Vanity URLs"
>
> So, their users use their username as their url.
>
> ex. www.mydomain.com/myusername  would route to their user profile.
>
> So my question is, how can you accomplish this with Zend Framework AND still
> have other controller/action pairs as well?
>
> Is there a way to setup a custom route where you check to see if that first
> param in the URL is a username, then if not.. attempt to use default
> routing?
>
> I have been using default routing completely in the site I am building so I
> am very new to the router.
>
> Thanks in advance for your help!
>  
Nick M

Re: Facebook Vanity URLs

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
Sorry sent too soon.

You can then add other standard routes, without then being matched by a greedy regex match:
routes.home.route = "home/*"
routes.home.defaults.controller = home
routes.home.defaults.action = index
 
routes.login.route = "login"
routes.login.defaults.controller = login
routes.login.defaults.action = index
 
routes.loginProcess.route = "logout"
routes.loginProcess.defaults.controller = login
routes.loginProcess.defaults.action = logout
 
routes.signup.route = "signup"
routes.signup.defaults.controller = signup
routes.signup.defaults.action = index


Nick M wrote:
I used the router route hostname and chained it with a standard router:

routes.user.type = "Zend_Controller_Router_Route_Hostname"
routes.user.route = "www.domain.com"

routes.user.chains.following.type = "Zend_Controller_Router_Route"
routes.user.chains.following.route = ":username/following/*"
routes.user.chains.following.defaults.controller = view
routes.user.chains.following.defaults.action = following

routes.user.chains.followers.type = "Zend_Controller_Router_Route"
routes.user.chains.followers.route = ":username/followers/*"
routes.user.chains.followers.defaults.controller = view
routes.user.chains.followers.defaults.action = followers

routes.user.chains.username.type = "Zend_Controller_Router_Route"
routes.user.chains.username.route = ":username/*"
routes.user.chains.username.defaults.controller = view
routes.user.chains.username.defaults.action = index



mapes911 wrote:
Hi all,

I've been wondering this for a while now about how Twitter/Myspace and now
Facebook creates what they are calling "Vanity URLs"

So, their users use their username as their url.

ex. www.mydomain.com/myusername  would route to their user profile.

So my question is, how can you accomplish this with Zend Framework AND still
have other controller/action pairs as well?

Is there a way to setup a custom route where you check to see if that first
param in the URL is a username, then if not.. attempt to use default
routing?

I have been using default routing completely in the site I am building so I
am very new to the router.

Thanks in advance for your help!
 
Mike Wright

Re: Facebook Vanity URLs

Reply Threaded More More options
Print post
Permalink
In reply to this post by Nick M
Nick M wrote:

> I used the router route hostname and chained it with a standard router:
>
> routes.user.type = "Zend_Controller_Router_Route_Hostname"
> routes.user.route = "www.domain.com"
>
> routes.user.chains.following.type = "Zend_Controller_Router_Route"
> routes.user.chains.following.route = ":username/following/*"
> routes.user.chains.following.defaults.controller = view
> routes.user.chains.following.defaults.action = following
>
> routes.user.chains.followers.type = "Zend_Controller_Router_Route"
> routes.user.chains.followers.route = ":username/followers/*"
> routes.user.chains.followers.defaults.controller = view
> routes.user.chains.followers.defaults.action = followers
>
> routes.user.chains.username.type = "Zend_Controller_Router_Route"
> routes.user.chains.username.route = ":username/*"
> routes.user.chains.username.defaults.controller = view
> routes.user.chains.username.defaults.action = index

Hi Nick,

Are these part of the same config file where resources are defined for
use by Zend_Application?

If so this might be a way to use the modular layout.  Each module's own
Bootstrap.php could contain its own routes and resources.

I'm still baffled by the new "resources" approach.  Every example helps
fill the void between my ears ;)

Thanx,
:m)
mapes911

Re: Facebook Vanity URLs

Reply Threaded More More options
Print post
Permalink
thanks for the suggestions guys!
i actually decided not to try the vanity url for users, but for businesses we are attempting to have a url like

http://<OUR DOMAIN>/business/<USERNAME>

we tried this

        $router = $frontController->getRouter(); // returns a rewrite router by default
       
        $defaults = array(
                'module' => 'default',
                'controller' => 'business',
                'action' => 'view');
       
        $businessRoute = new Zend_Controller_Router_Route('business/:username', $defaults);
       
        $router->addRoute('business', $businessRoute);

but when I go to
http://<DOMAIN>/business/testuser

I get the following
Error: username is not specified

#0 E:\library\Zend\Controller\Router\Rewrite.php(377): Zend_Controller_Router_Route->assemble(Array, true, true)

#1 E:\library\Zend\View\Helper\Url.php(49): Zend_Controller_Router_Rewrite->assemble(Array, NULL, true, true)
#2 [internal function]: Zend_View_Helper_Url->url(Array, NULL, true)
#3 E:\library\Zend\View\Abstract.php(318): call_user_func_array(Array, Array)

#4 [internal function]: Zend_View_Abstract->__call('url', Array)

any ideas what i'm doing wrong?

thanks
mapes911

Re: Facebook Vanity URLs

Reply Threaded More More options
Print post
Permalink
thanks to everyone that helped me figure this out.
for those that are interested, i wound up using an approach very similar to this posting

http://my.opera.com/zomg/blog/2007/09/19/extending-zend-framework-route-and-router-for-custom-routing


mapes911 wrote:
thanks for the suggestions guys!
i actually decided not to try the vanity url for users, but for businesses we are attempting to have a url like

http://<OUR DOMAIN>/business/<USERNAME>

we tried this

        $router = $frontController->getRouter(); // returns a rewrite router by default
       
        $defaults = array(
                'module' => 'default',
                'controller' => 'business',
                'action' => 'view');
       
        $businessRoute = new Zend_Controller_Router_Route('business/:username', $defaults);
       
        $router->addRoute('business', $businessRoute);

but when I go to
http://<DOMAIN>/business/testuser

I get the following
Error: username is not specified

#0 E:\library\Zend\Controller\Router\Rewrite.php(377): Zend_Controller_Router_Route->assemble(Array, true, true)

#1 E:\library\Zend\View\Helper\Url.php(49): Zend_Controller_Router_Rewrite->assemble(Array, NULL, true, true)
#2 [internal function]: Zend_View_Helper_Url->url(Array, NULL, true)
#3 E:\library\Zend\View\Abstract.php(318): call_user_func_array(Array, Array)

#4 [internal function]: Zend_View_Abstract->__call('url', Array)

any ideas what i'm doing wrong?

thanks