Custom routing namespace error

Bill Lions
Member | 47
+
0
-

Following the custom routing example on https://doc.nette.org/…tion/routing

I have created the following code:
namespace App;

use Nette\Http\IRequest as HttpRequest;
use Nette\Http\UrlScript;

class userRouter implements Nette\Routing\Router
{
        public function match(HttpRequest $httpRequest): ?array
        {
                $router = new RouteList;
                $router[] = new Route('/user', 'User:default');
                $router[] = new Route('/user/admin', 'User:admin');
                return $router;
        }

        public function constructUrl(array $params, UrlScript $refUrl): ?string
        {
                // ...
        }
}

Which produces an error:

Fatal Error
Interface ‘App\Nette\Routing\Router’ not found

Should this be pointing to another namespace?

Kamil Valenta
Member | 815
+
+1
-
class userRouter implements \Nette\Routing\Router
Bill Lions
Member | 47
+
0
-

kamil_v wrote:

class userRouter implements \Nette\Routing\Router

Even with the leading backslash, the issue remains.

1: <?php
2:
3: namespace App;
4:
5: use Nette\Http\IRequest as HttpRequest;
6: use Nette\Http\UrlScript;
7:
8: class userRouter implements \Nette\Routing\Router
9: {
10: public function match(HttpRequest $httpRequest): ?array
11: {
12: $router = new RouteList;
13: $router[] = new Route(‘/user’, ‘User:default’);
14: $router[] = new Route(‘/user/admin’, ‘User:admin’);
15: return $router;

Adding leading slashes to the use statements does not alleviate the error.

namespace App;

use \Nette\Http\IRequest as HttpRequest;
use \Nette\Http\UrlScript;

class userRouter implements \Nette\Routing\Router
{

Fatal Error
Interface ‘Nette\Routing\Router’ not found search►
Source file

File: …/html/app/UserModule/router/RouterFactory.php:8

1: <?php
2:
3: namespace App;
4:
5: use \Nette\Http\IRequest as HttpRequest;
6: use \Nette\Http\UrlScript;
7:
8: class userRouter implements \Nette\Routing\Router
9: {
10: public function match(HttpRequest $httpRequest): ?array
11: {
12: $router = new RouteList;
13: $router[] = new Route(‘/user’, ‘User:default’);
14: $router[] = new Route(‘/user/admin’, ‘User:admin’);
15: return $router;

Bill Lions
Member | 47
+
0
-

kamil_v wrote:

class userRouter implements \Nette\Routing\Router

Should this be as below?

\Nette\Application\IRouter

Last edited by Bill Lions (2020-07-13 01:58)

David Grudl
Nette Core | 8218
+
0
-

That code is meaningless. What are you trying to achieve?

Bill Lions
Member | 47
+
0
-

David Grudl wrote:

That code is meaningless. What are you trying to achieve?

The goal is to implement custom routes per module.
Then, intercept routes, and apply ACL to each route.

David Grudl
Nette Core | 8218
+
0
-

You probably want to create router create:

namespace App\Router;

use Nette\Application\Routers\RouteList;

class RouterFactory
{
	public static function createRouter(): RouteList
	{
		$router = new RouteList;
  		$router->addRoute('/user', 'User:default');
		$router->addRoute('/user/admin', 'User:admin');
		return $router;
	}
}
Bill Lions
Member | 47
+
0
-

David Grudl wrote:

You probably want to create router create:

Thanks.
How would this be invoked?
I tried adding it to the config under

services:
router: App\Router\RouterFactory::createRouter

but does not seem to load, and routes within do not work.
There is, of course, the router/RouterFactory.php file which contains the routes, which is defined as final.
If the routes are put in the ‘standard’ RouterFactory, they work fine.
Can there be only one RouterFactory?

David Grudl
Nette Core | 8218
+
0
-

Feel free to modify the ‘standard’ RouterFactory, that's exactly what it's designed for.

Bill Lions
Member | 47
+
0
-

David Grudl wrote:

Feel free to modify the ‘standard’ RouterFactory, that's exactly what it's designed for.

Ahh, thanks. I was thinking this was immutable.
I have renamed the class userRouterFactory, using the same code which you supplied, however, this produces the error:

Nette\MemberAccessException
Call to undefined method Nette\Application\Routers\RouteList::addRoute()

Not sure why this method is not found.

David Grudl
Nette Core | 8218
+
0
-

Are you sure you have Nette 3.0? It requires PHP 7.1 or newer.

Bill Lions
Member | 47
+
0
-

David Grudl wrote:

Are you sure you have Nette 3.0? It requires PHP 7.1 or newer.

Quite possibly could be an older version.
I cannot see the version in the readme.md.

Found version in composer..
will need to update and apply patch.
Thanks

>

“name”: “nette/application”,
“version”: “v2.4.12”,
“version_normalized”: “2.4.12.0”,

php -v
PHP 7.1.17

Last edited by Bill Lions (2020-07-14 07:05)

Bill Lions
Member | 47
+
0
-

Updated to latest version and working as expected.

Each module has its own RouterFactory as a stand-alone RouterList.