In CakePHP, is it possible to have internal and external routes?
The Facebook app is hosted on my server, for example, http://server.com/projects/fbapp/ , but it can only be viewed on Facebook, for example, http://apps.facebook.com/fbapp/ .
Using CakePHP presents a problem: should routes be prefixed with "/ project / fbapp" or just "fbapp"?
This is a problem because routes are not only used to route incoming requests, but also to generate links (and form actions, etc.).
As a kludge, I now have two route-by-route routing instructions:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('projects/fbapp/pages/*', array('controller' => 'pages', 'action' => 'display'));
The first time the prefix is missing because of the line I included in bootstrap.php:
Configure::write('App.base', '/fbapp');
What happens during reverse routing operations.
My question is, is there a more elegant way to do this? It seems very ugly and I'm sure it's not very Cakey.
a source to share
You don't have to do anything! Cake will take care of the base url for you, so you don't have to duplicate routes or worry about the base url. What you need to do is direct your relative URL instead of using projects/fbapp/
or whatever prefix you want to handle:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
This is how I handle all requests in my application that I deploy under http://www.example.com
and locally to http://localhost/workspace/example.com/trunk/deploy
. It works like a charm in both environments.
a source to share
Cross-posting my comment on request:
Wouldn't (apache) rewrite traffic from facebook with the most elegant solution? Your internal machine will only need to deal with one path, and if you want to integrate the application with another service / platform at a later date, you will need to add another rewrite rule rather than going into the application directly.
a source to share
I'll go based on Ask Apache and some of the rules I've set on a few old projects. I think putting it in a .htaccess file in your web court will do the trick (but that's an assumption).
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 correctly) rewrite any request corresponding to http://server.com/projects/fbapp/pages/ * to http://server.com/pages/ * ie http://server.com / projects / fbapp / pages / foo => http://server.com/pages/foo .
Edit Found this posted in the Apache section.
a source to share