How can I add a route to my TestPresenter?

Narinders_left_toe
Member | 2
+
0
-

Hello :] I'm new to Nette and I seem to have a problem understanding routing.
My goal is to create a link that redirects you from the page “generated” by the HomePresenter.php to the page by TestPresenter.php

I wanted to add it to my template like this: <a n:href="Test:test">create voucher</a>

My general directory structure looks like this:

site-name/
├── app
│   ├── Bootstrap.php
│   ├── Core
│   │   └── RouterFactory.php
│   └── UI
│       ├── Accessory
│       │   └── LatteExtension.php
│       ├── createvoucher
│       │   └── Test
│       ├── Error
│       │   ├── Error4xx
│       │   │   ├── 403.latte
│       │   │   ├── 404.latte
│       │   │   ├── 410.latte
│       │   │   ├── 4xx.latte
│       │   │   └── Error4xxPresenter.php
│       │   └── Error5xx
│       │       ├── 500.phtml
│       │       ├── 503.phtml
│       │       └── Error5xxPresenter.php
│       ├── createvoucher
│       │   ├── Test
│       │   │   ├── test.latte
│       │   │   └── TestPresenter.php
│       │   └── @layout.latte
│       └── voucherhistory
│           ├── Home
│           │   ├── default.latte
│           │   └── HomePresenter.php
│           └── @layout.latte
├── bin
├── composer.json
├── composer.lock
├── config
│   ├── common.neon
│   └── services.neon
├── log/
├── temp/
├── vendor/
│   └── autoload.php
└── www
    ├── css
    │   └── style.css
    ├── favicon.ico
    ├── index.php
    └── robots.txt

(I created the project via composer create-project nette/web-project PROJECT_NAME and didn't change that much of the file structure.)

My common.neon looks like this:

application:
    errorPresenter:
        4xx: Error:Error4xx
        5xx: Error:Error5xx
    mapping: App\UI\*\**Presenter

So far, my RouterFactory.php file looks like this


<?php

declare(strict_types=1);

namespace App\Core;

use Nette;
use Nette\Application\Routers\RouteList;


final class RouterFactory
{
        use Nette\StaticClass;

        public static function createRouter(): RouteList
        {
                $router = new RouteList;

                $basepath = "/lattest/voucher/www";

                #$router->addRoute($basepath.'Test/<action>', 'Test:test');

                $router->addRoute($basepath.'/<presenter>/<action>', 'Home:default');

                return $router;
        }
}

While this takes me to the correct site by default, I've been unable to successfully add the route to the TestPresenter.

[The server runs on Nginx]

Last edited by Narinders_left_toe (Yesterday 15:00)

m.brecher
Generous Backer | 842
+
0
-

@Narinders_left_toe

try change your code this way:

final class RouterFactory
{
   use Nette\StaticClass;

   public static function createRouter(): RouteList
   {
      $router = new RouteList;
      $basepath = "/lattest/voucher/www";
      $router->addRoute($basepath.'/test/<action>', 'Test:default');
      $router->addRoute($basepath.'/<presenter>/<action>', 'Home:default');
      return $router;
   }
}

changes:

  • url segment for TestPresenter should be separated from $basepath segment with /
  • url segment for TestPresenter should use only lower case characters and hyphens, so better test than Test
  • it is common practice use empty action url segment for action default, so thats why I propose use ‘Test:default’ instead of ‘Test:test’, action :test you can use without problem

Last edited by m.brecher (Yesterday 12:06)

Narinders_left_toe
Member | 2
+
0
-

Thank you for the answer!
I've changed everything accordingly but when i click on

<a n:href="Test:default">create voucher</a>

my URl turns into "/lattest/voucherapp-nette/www/test/" and I get an 404 Error again.

However, while trying this I noticed that the error message being displayed is the one from nginx and not the one from the error presenter. Which is not what I thought was supposed to happen ',:|

At this point, my colleagues and I are contemplating whether the problem might lie elsewhere.

m.brecher wrote:

@Narinders_left_toe

try change your code this way:

final class RouterFactory
{
   use Nette\StaticClass;

   public static function createRouter(): RouteList
   {
      $router = new RouteList;
      $basepath = "/lattest/voucher/www";
      $router->addRoute($basepath.'/test/<action>', 'Test:default');
      $router->addRoute($basepath.'/<presenter>/<action>', 'Home:default');
      return $router;
   }
}

changes:

  • url segment for TestPresenter should be separated from $basepath segment with /
  • url segment for TestPresenter should use only lower case characters and hyphens, so better test than Test
  • it is common practice use empty action url segment for action default, so thats why I propose use ‘Test:default’ instead of ‘Test:test’, action :test you can use without problem
nightfish
Member | 509
+
+1
-

@Narinders_left_toe

my URl turns into "/lattest/voucherapp-nette/www/test/" and I get an 404 Error again.

However, while trying this I noticed that the error message being displayed is the one from nginx and not the one from the error presenter.

Do you have the Nginx correctly set to support pretty URLs?

m.brecher
Generous Backer | 842
+
0
-

@Narinders_left_toe

$basepath in your router is like url prefix and you can use also route path prefix:

https://doc.nette.org/…tion/routing#…

final class RouterFactory
{
   use Nette\StaticClass;

   public static function createRouter(): RouteList
   {
      $router = new RouteList;
      $router->withPath('/lattest/voucher/www')
         ->addRoute('test/<action>', 'Test:default')
         ->addRoute('<presenter>/<action>', 'Home:default');
      return $router;
   }
}

But the second Route is fully capable manage also Test:default so you can simplify the router like this:

final class RouterFactory
{
   use Nette\StaticClass;

   public static function createRouter(): RouteList
   {
      $router = new RouteList;
      $router->addRoute('/lattest/voucher/www/<presenter>/<action>', 'Home:default');
      return $router;
   }
}

Last edited by m.brecher (Yesterday 19:45)