Symfony Multiple application interaction

非 Y 不嫁゛ 提交于 2020-01-01 03:14:07

问题


In symfony 1.4, how to call an action of another application from the current action?


回答1:


There's a blog post about that here:

  • http://symfony.com/blog/cross-application-links

There's a plugin for it:

  • https://github.com/rande/swCrossLinkApplicationPlugin

And there are some blogposts explaining it:

  • http://rabaix.net/en/articles/2009/05/30/cross-link-application-with-symfony
  • http://rabaix.net/en/articles/2009/07/13/cross-link-application-with-symfony-part-2

    (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. Add to your backend configuration the following two methods

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. You can link to your application in such a fashion now:

$this->redirect($this->getContext()->getConfiguration()->generateFrontendUrl('hello', array('name' => 'Bar')));

3. Since it's a bit tedious to write, you can create a helper

function link_to_frontend($name, $parameters)
{
  return sfProjectConfiguration::getActive()->generateFrontendUrl($name, $parameters);
}

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 ?>



回答2:


It would be something like this:

public function executeActionA(sfWebRequest $request)
{
  $this->redirect("http:://host/app/url_to_action");
}

In Symfony each application is independent from the others, so if you need to call an action of another app, you need to request it directly.

Each app is represented by one main controller (frontend, backend, webapp), this controller takes care of the delivery of each request to the corresponding action (and lots of other things like filters, etc.).

I really recommend you to read this, it would be quite more explanatory: Symfony - Inside the Controller Layer



来源:https://stackoverflow.com/questions/6368043/symfony-multiple-application-interaction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!