【发布时间】:2010-07-26 13:25:11
【问题描述】:
我有一个关于在 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 任务时不存在。
网址应该是这样的:
http://trunk.dev/frontend_dev.php/omg-news/omg-news-channel/test002.html
有没有人能够创建一个代表来自 symfony 任务的绝对 URL 的路由?如果有,您是否也遇到过这个问题,您是如何克服的?
【问题讨论】:
标签: symfony1