正在关注BetaRide的回答让我走上了正轨,但那还不够。然后它抱怨说:“无法为指定的路由生成一个URL”,因为这样的路由不存在。“
要创建我已经修改了它要求项目的根像这样一个有效的请求:(?确保根)
$request = new Request();
$request->create('/');
$this->container->enterScope('request');
$this->container->set('request', $request, 'request');
您可能需要调用不同的路线,根工作对我来说只是精细。
Symfony2 Docs
奖金此外:
我不得不通过Symfony2中做了这么多的模板/路由中的CLI命令,我已经更新AppKernel的initializeContainer()
方法。它创建到网站根目录的路径,设置路由器环境和假货用户登录:
protected function initializeContainer()
{
parent::initializeContainer();
if (PHP_SAPI == 'cli') {
$container = $this->getContainer();
/**
* Fake request to home page for cli router.
* Need to set router base url to request uri because when request object
* is created it perceives the "/portal" part as path info only, not base
* url and thus router will not include it in the generated url's.
*/
$request = Request::create($container->getParameter('domain'));
$container->enterScope('request');
$container->set('request', $request, 'request');
$context = new RequestContext();
$context->fromRequest($request);
$container->get('router')->setContext($context);
$container->get('router')->getContext()->setBaseUrl($request->getRequestUri());
/**
* Fake admin user login for cli. Try database read,
* gracefully print error message if failed and continue.
* Continue mainly for doctrine:fixture:load when db still empty.
*/
try {
$user = $container->get('fos_user.user_manager')->findUserByUsername('admin');
if ($user !== null) {
$token = $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->getContainer()->get('security.token_storage')->setToken($token);
}
} catch (\Exception $e) {
echo "Fake Admin user login failed.\n";
}
}
}
你可能并不需要最后$container->get('router')->getContext()->setBaseUrl($request->getRequestUri());
一部分,但我不得不这样做,因为我的网站根目录是在域.com/siteroot /和路由器正在剥离/ siteroot /为了生成网址。
这并不能解决问题。由于使用logn作为资产(可能不能直接在您的模板中可见),它需要以某种方式提供RequestContext服务。 – BetaRide 2013-01-11 11:08:05
我得到同样的错误,但此解决方案不能解决问题.http://stackoverflow.com/questions/17942738/erro-you-cannot-create-a-service-templating-helper-assets-of-an-inactive -sco – vishal 2013-07-30 09:22:32
当我发布它时,此解决方案适用于我。我不会争辩一下,它不适用于当时比较新版本的Symfony。谈到摆脱困境时,Symfony是一个动人的目标。我暂时没有时间编写代码,因此目前我无法调查,但是您可以尝试使用BetaRide的解决方案。 – Matthew 2013-08-05 00:24:24