Lately we’ve been looking for ways to build custom web applications faster. Our existing software is great but it’s not quite up to the calibre of some of the frameworks out there. After deciding on using Symfony to build our next generation software, and loving it, I set out to wrap our existing software in Symfony. As it turns out that was fairly easy thanks to Symfony’s routing system.
Creating a page in our existing CMS creates a PHP file in our document root, and those can be accessed without the .php extension thanks to mod_rewrite. I want Symfony to look in the document root directory, check if a file exists, and execute it if it does. If the URL is http://www.somesite.com/this-is-a-test I want to check if a file called this-is-a-test.php exists.
I added the following route late in routing.yml:
# apps/front/config/routing.yml ce_route: url: /* class: ceRoute
If no other routes match, then my route class ceRoute gets a chance to interpret the url. Here’s my route class:
// lib/routing/ceRoute.class.php class ceRoute extends sfRoute { public function matchesUrl($url, $context = array()) { // match the beginning of the url up to a semi colon, comma or forward slash if (!preg_match('!^/([^;,/]+)!', $url, $m)) return false; // if this file exists with the .php extension added on then it's // a page that exists $page = $m[1]; $script = sfConfig::get('app_cms_path')."/$page.php"; if (!is_file($script)) return false; return array( 'page_name' => $page, 'script' => $script, 'module' => 'contentEditor', 'action' => 'show' ); } }
If the route class finds a page that exists, some details about it are passed as paramters to the contentEditor module’s show action. Here’s a peek at that action:
// apps/front/modules/contentEditor/action/actions.class.php class contentEditorActions extends sfActions { public function executeShow(sfWebRequest $request) { $script = $request->getParameter('script'); ob_start(); include $script; $this->body = ob_get_clean(); } }
And the template for the action:
<!-- apps/front/modules/contentEditor/templates/showSuccess.php ?> <?php echo $sf_data->getRaw('body') ?>
This works great for us. Now we can use Symfony’s cache system, security system, and all of it’s other great features without having to port our existing code to Symfony. Obviously this won’t work for everyone but I hope it helps others in a similar situation.





