Skip to content

Add a Page

Mohamad Zbib edited this page Aug 10, 2021 · 1 revision

How to Add a Page

Create A controller

go to folder app/Controller and Create new JS file and with a class inside of it. the class name should be the same as the file name.

import view from common file

import { view } from "../../system/common.js";

the default method is usually called index, and all the methods have an argument called data as a JSON object.

Create the index or other methods

index = data => {return "the response message"}
otherMehodName = data => {return "the response message of the other method name"}

Adding controller to the route

simply you can add routes by going to the file app/Config/Routes.js, and use router.get() or router.post()

router.get(_routeUrl_, _ClassMethodString_)

if our class if named TestClass:

//to access index method
router.get('/test', 'TestClass')
// or you can use
router.get('/test', 'TestClass::index')

//to access `otherMehodName`
router.get('/other/method/route', 'TestClass::otherMehodName')

Access Get data from url

to get information or GET data from the url, simply use {var_name} in the route

router.get('/other/method/route/{id}/{date}', 'TestClass::otherMehodName')

so to access id and date from your method just use data.id and data.date

render view using ejs

to render the view from your method use return view(...)

view(_viewFile_, _jsonData_)

you should create your view file in app/View and save it as ejs, like my_view.ejs

in your method use

return view('my_view',{title:"my title"})

and in your file simply use your data like

<h1> <%= title %> </h1>