Register Web & API Routes For MultiStore

Each module has by default its web and API routes loaded on NexoPOS 4.x once it's enabled. However, if you would like your module to be working within a sub-store, you'll need to register your routes (web and API) for the MultiStore module.

Throughout his life cycle, the Multi-Store module triggers various events that can be used to include a module route that will be extended by the multi-store module. It will extend these by adding a prefix on each route and route name.

How To Register An API & Web Route

Within your service provider, you need to listen to the event "Modules\NsMultiStore\Events\MultiStoreWebRoutesLoadedEvent" and "Modules\NsMutliStore\Events\MultiStoreApiRoutesLoadedEvent" in order to include your routes file. Here is how you'll proceed.

How To Create Prefixed Route Name

If your route uses a static name, then on multistore, only the last store route will overwrite others. This means, for example, if you have 2 stores, the multistore module will generate routes like this.

// store 1 route

// store 2 route

If the same name is used for both routes, then by calling "route" helper, only the route for the second route will be returned. We, therefore, need to provide a prefix for the route's name. Here is how you'll proceed :

Note that here, you don't need to specify the "/dashboard" prefix as all modules routes, either on web.php or api.php are prefixed by "/dashboard" already.

How To Use Prefixed Route Name

Now it might be useful to know how you can use these route names as using the traditional "route()" function helper won't work. We'll use another helper available in the CoreService class, that is : ns()->route(). This function is extended by the Multi-Store module and inject a store prefix whenever the function is called within a store.

Here is how you'll do it while creating a menu.