Skip to content

Many modules on one server

A module is a program with its own entry file. One server can run many modules, and one module can link to a page of another module.

apps/
nvs.toml
Blog/
public/index.nvs the entry file of Blog
src/View/Post.nvs the class Framework\Blog\View\Post
Shop/
public/index.nvs the entry file of Shop
src/View/Item.nvs the class Framework\Shop\View\Item

A [[server.mount]] block with scan serves every entry file that matches the pattern. {1} is the text that the first * matched. {1:lower} is the same text in lower case.

[server]
root = "."
[[server.mount]]
host = "www.example.com"
scan = "*/public/index.nvs"
prefix = "/{1:lower}"
origin = "https://www.example.com"

This block serves Blog at /blog and Shop at /shop. origin is optional. A module on another host needs it to link to these modules.

Each module has its own entry file. The line autoload 'Framework\{..}' from '../src'; loads the classes of this module from its own src folder.

The line autoload links discover 'Framework' from '../../*/src'; reads the classes of every module. Novis reads them only for their routes. It does not compile them, and your program cannot call them. You can import such a class with use, so a link can use its short name.

Many modules can share one framework folder. A package.toml with a [package] name makes that folder a package. Every file in it must use the package’s namespace.

apps/
Framework/
package.toml [package] name = 'Framework\Core'
src/Signing/Url.nvs the class Framework\Core\Signing\Url
Blog/
public/index.nvs the entry file of Blog

The framework’s package.toml can have a [[restrict]] table. It says where a class or a method may be used. This entry allows Core\Router::urlSigned only in the framework’s own signing code:

[package]
name = 'Framework\Core'
[[restrict]]
use = 'Core\Router::urlSigned'
allow = ['Framework\Core\Signing\*']
note = "Use `Framework\Core\Signing\Url::signed`. It adds the framework's key rotation."
replace = 'Framework\Core\Signing\Url::signed($route, $params)'

The restriction holds in every module that loads a file of the framework. A call to Core\Router::urlSigned in Blog does not compile. The error shows the note, and the editor offers the replace code as a fix. A module cannot remove the restriction in its own package.toml.

use and allow are required. use names one class, method, property, constant, function or namespace. allow lists the namespaces, classes and methods that may use it. Ns\* means the namespace Ns and every namespace below it. An empty list, allow = [], allows no place. note and replace are optional.

A package can restrict its own code and code in Core. It cannot restrict another package or a module, and such an entry is error E0665.

Core\Router::url returns the link to a route of another module. The compiler checks the link. The method must exist and have a #[Route]. Each {...} part of the route path needs a value in the array.

<?nvs
// Shows a link from this program to a page of the Shop module.
// `autoload links` reads the routes of every module in the `apps` folder.
// Your program can link to these routes, but it cannot call their code.
autoload links discover 'Framework' from 'apps/*/src';
use Framework\Shop\View\Item;
// This program and Shop are on the same host, so the link is a path.
// The server serves Shop at `/shop`. This prints "/shop/item/3".
echo Core\Router::url(Item::show, ['id' => 3]), "\n";
nvs serve (HTTP Server Response)
/shop/item/3

When the module is on another host, the link starts with the origin of its mount. Without an origin, no link from another host can reach the module. Core\Router::urlAbsolute always starts the link with an origin.

<?nvs
// Shows a link from a program on `help.example.com` to a module on `www.example.com`.
autoload links discover 'Framework' from 'apps/*/src';
use Framework\Shop\View\Item;
// Shop is on another host, so the link starts with the `origin` of its mount.
// This prints "https://www.example.com/shop/item/3".
echo Core\Router::url(Item::show, ['id' => 3]), "\n";
nvs serve (HTTP Server Response)
https://www.example.com/shop/item/3

A module can have more than one mount. Then the server chooses which mount a link uses. It takes the first of these that serves the route:

  1. A mount from the same [[server.mount]] block. All modules of one scan are one block.
  2. A mount on the same host. The link is a path.
  3. A mount on another host that has an origin. The link starts with that origin.

Two mounts can match at the same step. Then the server uses the block with canonical = true:

[[server.mount]]
host = "www.example.com"
prefix = "/journal"
entry = "Blog/public/index.nvs"
canonical = true
<?nvs
// Shows how `canonical = true` picks one mount when a module has two.
// The server serves Blog at `/blog` and at `/journal`.
autoload links discover 'Framework' from 'apps/*/src';
use Framework\Blog\View\Post;
// The `/journal` mount has `canonical = true`, so the link uses it.
// This prints "/journal/post/5".
echo Core\Router::url(Post::show, ['id' => 5]), "\n";
nvs serve (HTTP Server Response)
/journal/post/5
  • If a link needs this choice and no block has canonical = true, the server does not start. It also does not start when two such blocks match.
  • Two canonical = true blocks for one entry file are always an error.
  • Core\Router::urlSigned cannot link to another module. A signed link works only inside one module.

nvs config check --mounts prints every mount. Under each mount, it prints the mount each link goes through:

Terminal: apps
nvs config check --mounts nvs.tomlmount `/blog` on host `www.example.com`: `~/project/Blog/public/index.nvs` (block 1)  link `Framework\Shop\View\Item::show` goes through `/shop`mount `/journal` on host `www.example.com`: `~/project/Blog/public/index.nvs` (block 2, canonical)  link `Framework\Shop\View\Item::show` goes through `/shop`mount `/shop` on host `www.example.com`: `~/project/Shop/public/index.nvs` (block 1)  link `Framework\Blog\View\Post::show` goes through `/blog`ok: 1 file, 9 directives set, 0 overrides, 0 warnings

Read Configuration for the other blocks of nvs.toml.