關於在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