How do I make a route use a parameter value from the current url when building a link to the same presenter?

5 years ago

petr.pavel
Backer | 492
+
0
-

My application serves both desktop and mobile version of a website, for both the server and my workstation (fake tld .pp).

I define routes for the mobile version with the server name (routing-test.pp / routing-test.com in this case). When generating a link to another action of the same presenter, I expected the parameter <domain> to be taken from the current request, just as with the presenter name.

It's not the case though. I only get a match on my route when I provide the domain value specifically.

Can anyone explain to me how I could achieve what I want, or at least why the current behaviour is correct and not a bug?

I tried to replicate with the dev version but I failed to make my test app work (something error with IRouter).

<?php
  public function createRouter() {
    $router = new RouteList();
    $router[] = $moduleRouter = new RouteList('Mobile');

    $moduleRouter[] = new Route("//routing-test.<domain>/bg[/<id>]", 'Page:bg');
    $moduleRouter[] = new Route("//routing-test.<domain>/", 'Page:default');  // home page

    return $router;
  }
?>
<?php
  public function renderDefault()  {
    // ------------------------------------------------------
    print $this->link(':Mobile:Page:bg', array('id' => 1, 'domain' => 'pp')).'<br>'; // works
    print $this->link(':Mobile:Page:bg', array('id' => 1)).'<br>'; // no route found
    exit;
  }

  public function renderBg($id)  {
    // ------------------------------------------------------
  }
?>

Nette Framework 2.0.13 (revision 695f643 released on 2013–11–05)

Here's the whole test app.

Last edited by petr.pavel (2013-12-03 13:43)

5 years ago

Jan Tvrdík
Nette guru | 2547
+
0
-

This is obviously correct behavior, use persistent parameters.

5 years ago

petr.pavel
Backer | 492
+
0
-

I confirm that adding a persistent $domain parameter to my presenter removes the necessity to pass domain to links manually. Thanks.

<?php
  /**
   * @persistent
   */
  public $domain;
?>

I somehow thought that parameters extracted from the domain name would be preserved automatically because well, that's what we always want, right?
But I understand now that they're treated no different from parameters extracted from the path ($id in my case).

5 years ago

Quinix
Member | 108
+
0
-

Just FYI, in dev Nette, there are special parameters for this routes – %basePath%, %domain%, %tld% – which are automatically added to links, so there is no longer need for this workaround.

https://github.com/…rs/Route.php#L153