views:

357

answers:

3

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.

+1  A: 

Cross posting my comment by request:

Wouldn't an (apache) rewrite of traffic from facebook be the most elegant solution? Your internal machine would only have to deal with one path, and if you wanted to integrate the app with another service/platform at a later date, you would only have to add another rewrite rule rather than messing with the application itself.

jacobangel
You don't need this. And no, it won't be the most elegant solution, because if you don't work with Apache it won't work. In fact, you don't need to do anything; look at my answer.
Seb
+1  A: 

I'll have a go, based on Ask Apache and some rules I've put in place on a few old projects. I think putting it in the .htaccess file in your webroot would do the trick (but that's a guess).

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/projects/fbapp/pages.*
RewriteRule ^/projects/fbapp/pages/(*)$ /pages/$1 [L]

The above should (if I did it right) rewrite any request matching http://server.com/projects/fbapp/pages/* to http://server.com/pages/* i.e. http://server.com/projects/fbapp/pages/foo => http://server.com/pages/foo.

Edit Found this posted in the Apache section.

ajm
You don't need this and, moreover, if you don't work with Apache it won't work. In fact, you don't need to do anything; look at my answer.
Seb
+1  A: 

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:

Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

That's how I handle all requests in my app, which I deploy under http://www.example.com, while locally I have it under http://localhost/workspace/example.com/trunk/deploy. It works like a charm in both environments.

Seb