Symfony1 在symfony任务中生成绝对URL
我有一个关于在symfony任务中创建绝对URL的快速问题。 基本上,我有以下几点:Symfony1 在symfony任务中生成绝对URL,symfony1,Symfony1,我有一个关于在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
/**
* 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时。
在任务中创建路由时,我得到以下信息:
我的假设是,symfony试图从推荐人那里猜出域名,而使用symfony任务时,域名是不存在的
URL应如下所示:
是否有人能够从symfony任务创建表示绝对URL的路由?如果是这样,您是否也遇到了这个问题,您是如何克服的?请参阅我在哪里发布的:
此方法被添加到我们的基本任务类中,我们在其中添加了其他常见的特定于项目的任务方法。回答了这个问题。编辑factories.yml配置文件就足够了:
all:
routing:
class: sfPatternRouting
param:
generate_shortest_url: true
extra_parameters_as_query_string: true
context:
host: example.org
看起来您也只能在任务中欺骗路由:
sfConfig::set('sf_factory_request_parameters', array('relative_url_root' => "", 'no_script_name' => true));
sfContext::createInstance($this->configuration);
这样,您就不必更改主配置。这是一个完美的答案!现在我甚至可以在任务中为使用
url\u!我喜欢这个答案,因为它很简单。请记住,主机在dev
和prod
环境中可能不同。请同意@Tapper,这只应被视为黑客行为。
sfConfig::set('sf_factory_request_parameters', array('relative_url_root' => "", 'no_script_name' => true));
sfContext::createInstance($this->configuration);