A Facebook app is hosted on my server at, say, http://server.com/projects/fbapp/, but is only ever viewed in Facebook at, for instance, http://apps.facebook.com/fbapp/.
Using CakePHP this presents a problem – should routes be prefixed with “/project/fbapp” or just “fbapp”?
It’s a problem because routes are used not just for routing inbound requests, but also for generating links (and form actions etc).
As a kludge, I now have two routing instructions per route:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('projects/fbapp/pages/*', array('controller' => 'pages', 'action' => 'display'));
With the first not requiring a prefix because of a line I’ve included to bootstrap.php:
Configure::write('App.base', '/fbapp');
Which kicks in during reverse routing operations.
My question is whether there’s a more elegant way to do this? This seems very ugly and I’m sure it’s not very Cakey.
You don’t need to do anything at all! Cake will take care of base URL for you, so you don’t need to duplicate the routes, nor take care of the base URL. What you need to do is to route your relative URL instead of using
projects/fbapp/, or whatever prefix you want to handle:That’s how I handle all requests in my app, which I deploy under
http://www.example.com, while locally I have it underhttp://localhost/workspace/example.com/trunk/deploy. It works like a charm in both environments.