Route abbreviation understanding

In the developer manual, there are several mentions about routes abbreviation. For example in notes tutorial section:

Now the controller methods need to be connected to the corresponding URLs in the notestutorial/appinfo/routes.php file:

<?php
return [
    'routes' => [
        ['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
        ['name' => 'note#index', 'url' => '/notes', 'verb' => 'GET'],
        ['name' => 'note#show', 'url' => '/notes/{id}', 'verb' => 'GET'],
        ['name' => 'note#create', 'url' => '/notes', 'verb' => 'POST'],
        ['name' => 'note#update', 'url' => '/notes/{id}', 'verb' => 'PUT'],
        ['name' => 'note#destroy', 'url' => '/notes/{id}', 'verb' => 'DELETE']
    ]
];

Since those 5 routes are so common, they can be abbreviated by adding a resource instead:

<?php
return [
    'resources' => [
        'note' => ['url' => '/notes']
    ],
    'routes' => [
        ['name' => 'page#index', 'url' => '/', 'verb' => 'GET']
    ]
];

My question is: isn’t there routing information loss? With the second codeblock, how does the server know, which controller method (in this case method update()) to use when user inserts request PUT with /notes/{id} in the URL?

In the first codeblock it’s clear → it just matches the url and the verb and uses route “note#update” (which corresponds to NoteController::update()).

But in the second codeblock, I don’t see how the server would know to call the same method with the same request.

Thanks.

image

Solved by Raimund SchlĂĽĂźler on dev chat.

For posterity it would be cool if you could briefly describe the solution for for other devs stumbling over the same topic :v:

I guess it’s rather explanation than solution. Information loss mentioned by me is simply not the case, because few routes are hard-coded in the AppFramework.

Specifically index, show, create, update and destroy. These routes do not have to be specified in routes, but their prefix (which will be name of the controller) need to be specified in resources. Their mapping to Controller methods is automatic if I undestand it correctly.

However I think this creates more confusion on when the abbreviated routes into resources is actually useful outside of these few cases.

Also, this should be put inside a Developer manual since the explanation had to be found deep inside a source code.

1 Like

https://docs.nextcloud.com/server/stable/developer_manual/basics/routing.html#registering-resources covers this, doesn’t it?