关于在symfony任务中创建绝对URL的问题,我有一个疑问。 基本上我有以下几点:在symfony任务中生成绝对URL
/**
* This function returns the link to a route
*
* @param $context context from which to create the config from
* @param $routingText this contains the text to be routed
* @param $object if we are generating an object route we need to pass the object
* @param boolean $absolute - whether to generate an absolute path
* @return string
*/
public static function getUrlFromContext($routingText, $object = null, $application = null, $debug = false,
$absolute = false, $htmlSuffix=true)
{
$currentApplication = sfConfig::get('sf_app');
$currentEnvironment = sfConfig::get('sf_environment');
$context = sfContext::getInstance();
$switchedContext = false;
if (!is_null($application) && $context->getConfiguration()->getApplication() != $application)
{
$configuration = ProjectConfiguration::getApplicationConfiguration($application, $currentEnvironment,
$debug);
$routing = sfContext::createInstance($configuration)->getRouting();
$switchedContext = true;
}
else
{
$routing = $context->getRouting();
}
if (is_object($object))
{
$route = $routing->generate($routingText, $object, $absolute);
}
else
{
$route = $routing->generate($routingText, null, $absolute);
}
if ($switchedContext)
{
sfContext::switchTo($currentApplication);
}
if (strcasecmp($application, 'frontend') == 0 && strcasecmp($currentEnvironment, 'prod') == 0)
{
$route = preg_replace("!/{$currentApplication}(_{$currentEnvironment})?\.php!", $application, $route);
}
else
{
$route = preg_replace("/$currentApplication/", $application, $route);
}
return $route;
}
这让我简单地通过切换背景下创建的任何应用程序的URL。我遇到的最大问题是在symfony任务中创建绝对URL。 当任务创建路由我得到以下几点:
http://./symfony/symfony/omg-news/omg-news-channel/test002.html
我的假设是,symfony的是试图猜测从指引,其使用symfony的任务时,该域名是不可-existent。
的URL应该是这样的:
http://trunk.dev/frontend_dev.php/omg-news/omg-news-channel/test002.html
有没有人能够创造出代表从symfony的任务绝对URL路径?如果是的话,你也遇到了这个问题,你是如何克服它的?
这是最完美的答案!现在我甚至可以在任务中使用'url_for'! – flocki 2012-07-17 12:46:30
我喜欢这个答案,因为它很简单。请记住主机在'dev'和'prod'环境中可能会有所不同。 – Tapper 2012-10-30 14:25:50
同意@Tapper,这应该只被视为黑客。 – 2015-01-15 01:07:32