Routes and Navigation
Routes are used to handle URL navigation by matching the requested URL from the browser to a router pattern in your plugin.
Types of Routes
There two (2) types of routes:
Plugin routes
- accessible to all usersAdmin routes
- accessible to authenticated admin accounts.
Plugin Routes
Below is an example on how to register a route to the plugin router.
Any route registered to the plugin router are categorized as plugin route
.
package main
import (
"net/http"
sdkapi "sdk/api"
)
func main() {}
func Init(api sdkapi.PluginApi) {
pluginRouter := api.Http().HttpRouter().PluginRouter()
pluginRouter.Get("/welcome/:name", func (w http.ResponseWriter, r *http.Request) {
vars := api.Http().MuxVars(r)
name := vars["name"]
welcomePage := views.WelcomePage(name)
api.Http().HttpResponse().PortalView(w, r, sdkapi.ViewPage{
PageContent: welcomePage,
})
}).name("portal:welcome")
}
package views
templ WelcomePage(name string) {
<p>Welcome, { name }</p>
}
In this example, we registered a plugin route named portal:welcome
that executes when a user navigates to /welcome/:name
.
Then we extract the name
URL param using IHttpApi.MuxVars method and display the welcome.templ
view template.
The plugin router has additional methods aside from Get
. See the Router Instance documentation.
Admin Routes
Admin routes are very similar to plugin routes, but are only accessible by authenticated admin accounts.
Below is an example on how to register an admin route. Any route registered to the admin router are categorized as admin route
.
package main
import (
"net/http"
sdkapi "sdk/api"
)
func main() {}
func Init(api sdkapi.PluginApi) {
adminRouter := api.Http().HttpRouter().AdminRouter()
adminRouter.Get("/welcome/:name", func (w http.ResponseWriter, r *http.Request) {
vars := api.Http().MuxVars(r)
name := vars["name"]
welcomePage := views.WelcomePage(name)
api.Http().HttpResponse().AdminView(w, r, sdkapi.ViewPage{
PageContent: welcomePage,
})
}).name("admin:welcome")
}
See the Router Instance documentation for the details.
Portal menu item
In the example above, we registered a plugin route to the plugin router. We named the route portal:welcome
. Take note of the name of the route since we will use it as reference when registering a portal menu item.
To add a portal menu item that links to portal:welcome
route, we will use the INavsApi.PortalNavsFactory method.
// rest of the init function code...
navsAPI := api.Http().Navs()
navsAPI.PortalNavsFactory(func(r *http.Request) []PortalNavItemOpt {
return []sdkapi.AdminNavItemOpt{
{
Label: "Welcome", // Menu display text
RouteName: "portal:welcome", // Link to the route
IconUrl: api.Http().Helpers().ResourcePath("assets/images/some-image.jpg"),
RouteParams: map[string]string{
"name": "John",
},
},
}
})
Now, visit localhost:3000 to see if the menu item appears.
Admin menu item
In the example above, we also registered an admin route to the admin router. We named the route admin:welcome
. Take note of the name of the route since we will use it as reference when registering an admin menu item.
To add an admin menu item that links to admin:welcome
route, we will use the INavsApi.AdminNavsFactory method.
// rest of the init function code...
navsAPI := api.Http().Navs()
navsAPI.AdminNavsFactory(func(r *http.Request) []AdminNavItemOpt {
return []sdkapi.AdminNavItemOpt{
{
Category: sdkapi.NavCategorySystem, // Category of the menu item
Label: "Welcome", // Menu display text
RouteName: "admin:welcome", // Link to the route
},
}
})
Now, visit localhost:3000/admin to see if the admin menu item is present.