问题描述
在symfony 1.4中,如何从当前动作调用另一个应用程序的动作?
In symfony 1.4, how to call an action of another application from the current action?
推荐答案
这里有一篇博文:
有一个插件:
还有一些博文对此进行了解释:
And there are some blogposts explaining it:
http://rabaix.net/en/articles/2009/07/13/cross-link-application-with-symfony-part-2
(注意:两者都适用于 symfony 1.2,但它们也应该适用于 1.4)
(Note: both are for symfony 1.2, but they should also work in 1.4)
要将前端路由到后端,只需三个简单步骤:
To route to your frontend to your backend, there are three easy steps:
1.将以下两种方法添加到您的后端配置
这些方法读取后端路由,并使用它来生成路由.您需要提供指向它的链接,因为 php 不知道您如何为其他应用程序配置您的网络服务器.
These methods read the backend routing, and use it to generate routes. You'll need to supply the link to it, since php does not know how you configured your webserver for the other application.
.
// apps/backend/config/backendConfiguration.class.php
class backendConfiguration extends sfApplicationConfiguration
{
protected $frontendRouting = null;
public function generateFrontendUrl($name, $parameters = array())
{
return 'http://frontend.example.com'.$this->getFrontendRouting()->generate($name, $parameters);
}
public function getFrontendRouting()
{
if (!$this->frontendRouting)
{
$this->frontendRouting = new sfPatternRouting(new sfEventDispatcher());
$config = new sfRoutingConfigHandler();
$routes = $config->evaluate(array(sfConfig::get('sf_apps_dir').'/frontend/config/routing.yml'));
$this->frontendRouting->setRoutes($routes);
}
return $this->frontendRouting;
}
// ...
}
2.您现在可以以这种方式链接到您的应用程序:
$this->redirect($this->getContext()->getConfiguration()->generateFrontendUrl('hello', array('name' => 'Bar')));
3.由于写起来有点繁琐,可以创建一个helper
function link_to_frontend($name, $parameters)
{
return sfProjectConfiguration::getActive()->generateFrontendUrl($name, $parameters);
}
sfCrossLinkApplicationPlugin 这样做,这样做,但以更简单的方式,您将能够使用类似这样的语法:
The sfCrossLinkApplicationPlugin does this , this, but in a bit simpler fashion, you would be able to use a syntax similar to this:
<?php if($sf_user->isSuperAdmin()):?>
<?php link_to('Edit Blog Post', '@backend.edit_post?id='.$blog->getId()) ?>
<?php endif ?>
这篇关于Symfony 多应用交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!