How can I bootstrap Symfony into a standalone script?
(I hope 'bootstrap' is the correct term...)
I have a Symfony 1.4 project in which I'm using a PHP script that mostly contain开发者_开发百科s Javascript (I'm including this script with a simple <script src="/js/myStuff.js"></script>
tag). I need to use some Symfony classes, helper methods, and variables from within the script (specifically the sfConfig
class, url_for()
helper method, and the $sf_request
variable.) I'm at a loss as to how to achieve this. I tried copying the code from one of the front controllers into the script, but that ended up outputting the contents of my application's layout file.
Thanks in advance!
You can do what you want by using something like this to create a symfony context:
require_once($_SERVER['DOCUMENT_ROOT'].'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
$context = sfContext::createInstance($configuration);
To use url_for, you will also need to either load/include the Url helper, which can be done like:
sfContext::getInstance()->getConfiguration()->loadHelpers('Url');
I think there's a better approach though:
Serve this javascript file as symfony action if you need access to symfony - there's nothing that says you can only serve html through symfony.
Check out the block here entitled Javascript As An Action for an explanation ...
http://www.symfony-project.org/jobeet/1_2/Doctrine/en/18#chapter_18_user_feedback
精彩评论