8

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

2 Answers2

7

There's a blog post about that here:

There's a plugin for it:

And there are some blogposts explaining it:

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 ?>
Community
  • 1
  • 1
Arend
  • 3,741
  • 2
  • 27
  • 37
1

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

Craig Anderson
  • 754
  • 1
  • 11
  • 18
guiman
  • 1,334
  • 8
  • 13
  • Totally agreed, i did not say it was the best one, but it is a way, and of you look at the artibles posted by you, you'll see that they are still using concatenation of text to generate an absolute url. – guiman Jun 16 '11 at 17:54
  • Of course, in the end that's what the routing systems does :) – Arend Jun 19 '11 at 17:44