How to get App-URL

How do I get the URL of my own App?

I need it in the navigation part of the template. Currently I use relative paths, but this does not work any more, because I step deeper:

Currently I use:

<ul>
  <li><a href="geschaefte">Offene Geschäfte</a></li>
  <li><a href="fraktionssitzungen">Fraktionssitzung</a></li>
  <li><a href="ggrsitzungen">GGR Sitzungen</a></li>
</ul>

This corresponds to the following routes:

return [
    'routes' => [
      ['name' => 'geschaeft#index', 'url' => '/', 'verb' => 'GET'],
      ['name' => 'geschaeft#index', 'url' => '/geschaefte', 'verb' => 'GET'],
      ['name' => 'geschaeft#update', 'url' => '/geschaefte/{id}', 'verb' => 'PUT'],
      ['name' => 'ggrsitzungen#index', 'url' => '/ggrsitzungen', 'verb' => 'GET'],
      ['name' => 'geschaeft#ggrsitzung', 'url' => '/ggrsitzung/{id}', 'verb' => 'GET'],
      ['name' => 'fraktionssitzungen#index', 'url' => '/fraktionssitzungen', 'verb' => 'GET'],
    ]
];

But now I must change it to absolute paths since I have subpathes such as ggrsitzung/{id} and from there the relative path does not work anymore.

How?

I only found this solution, but I don’t think this is the intended way, especially since \OCP\Util->linkTo is deprecated:

<?php
use \OCP\Util;
$util = new Util();
?>
<ul>
  <li><a href="/index.php<?php echo $util->linkTo('ggrwinti'); ?>geschaefte">Offene Geschäfte</a></li>
  <li><a href="/index.php<?php echo $util->linkTo('ggrwinti'); ?>fraktionssitzungen">Fraktionssitzung</a></li>
  <li><a href="/index.php<?php echo $util->linkTo('ggrwinti'); ?>ggrsitzungen">GGR Sitzungen</a></li>
</ul>

In general people use a lot of JS because an app requires interactivity, so there’s OC.generateUrl() like here https://github.com/nextcloud/app-tutorial/blob/master/js/script.js#L192

If you want to keep it in your non JS templates, then there are two options:

  • Inject and pass the IURLGenerator to the template and use it from there (easy but meh)
  • Inject the IURLGenerator in your controller and resolve all required URLs, then pass them as an assoc array to your template

So bascically you want someting like:

$_['routes']['yourapp.controller.method']

That produces an empty string, does not work:

<p>URL: "<?php echo $_['routes']['ggrwinti.geschaeft.index']; ?>"</p>

And moreover, I’d like t specify the URL, not the routing, the routing would rather be the task of the appinfo/routes.php.

JavaScript makes sense, but not everywhere. JavaScript for navigation is a no-go. Basic functionality should not depend on JavaScript, IMHO.

Yeah, you need to create the routes array ofc, its not there from the beginning. Using the route name is “cleaner” since things don’t break if your URLs break.

JavaScript makes sense, but not everywhere. JavaScript for navigation is a no-go. Basic functionality should not depend on JavaScript, IMHO.

While this is probably true for websites I’d challenge that for your current usecase.

How? What means «ofc»?

Short for “Of course”

How?

That is exactly my initial question …

Links your request to the correct Controller file + method.

That does not help.

What I need is a full-URL including at least path prefix, or anything else that might be necessary, so I need something like: https://my.domain.com/path/to/my/nextcloud/installation/index.php/appname/…

The only way I found to get it, is \OCP\Util->linkTo, which is deprecated.

So how is it meant to get the full URL, at least the full relative path to the app?

As mentioned before inject IURLGenerator then do something like that:

public function __construct(/* more stuff before here */ OCP\IURLGenerator $urlGenerator) {
    $this->urlGenerator = $urlGenerator;
}

/* in the controller method */

return new TemplateResponse($this->app, 'template_name', ['routes' => [
    $this->app . '.page.index' => $this->urlGenerator->linkToRoute($this->app . '.page.index')
]]);

I wrote this from memory so you might run into syntax errors but it should demonstrate the usage

@BernhardPosselt, that’s still not the full answer. Where do I get the OCP\IURLGenerator from? That’s an abstract base class and simply adding an argument to the controller’s construtor leads to a crash in Application.php, where it is instanciated:

Nachricht: Argument 6 passed to OCA\GgrWinti\Controller\GeschaeftController::__construct() must implement interface OCP\IURLGenerator, none given, called in /var/www/nextcloud/apps/ggrwinti/lib/AppInfo/Application.php on line 64

If you don’t add the controller in Application.php then it will be constructed by convention (by looking at your type hints). If you do it manually you want to do something like $c->getServer()->getURLGenerator() (see https://github.com/nextcloud/server/blob/master/lib/private/Server.php#L1291)

BTW, if you want to chat join #nextcloud-dev on irc.freenode.net

That chat is dead.

Not really, its just IRC. Not everyone is monitoring the chat window at all times, just ask your question and await an answer

I opened it yesterday, 20:10 o’clock, now it’s 10:05. In these 14 hours, there was not a single message execpt two «hi» and what I wrote. Only tons of enter/quits.

No, that channel is completely dead and useless!

Nah, totally normal. Think about IRC as easier group E-Mail.