3. “Hello world!” User interface¶
Our goal in the chapter is to create a simple webpage which can execute our “hello world” service and return the results on screen.
For this, we use the standard Phalcon components to present the user with a form and need to create a controller which will bind a template to show.
Create a controller¶
The routing of user pages function the same as api endpoints, only the location of the file is one level up in the tree. For this example we’re going to create the following file:
1<?php
2namespace ValueA\Samples;
3
4class HelloworldController extends \ValueA\Base\IndexController
5{
6 public function indexAction()
7 {
8 $this->view->title = 'Helloworld!';
9 $this->view->pick('ValueA/Samples/helloworld');
10 }
11}
In line 8 the title is set for usage in our standard template, line 9 tells the system which template it should render, also mind the name of the controller, this should be the same as the filename without the extension (like the api controller).
Create a template (view)¶
Now we’re ready to build our html template, but before we do so we need to make sure the directory exists.
mkdir -p /usr/local/opnsense/mvc/app/views/ValueA/Samples
1<script type="text/javascript">
2$( document ).ready(function() {
3 $("#hello_btn").click(function(){
4 ajaxGet(url="/api/samples/hello/world", sendData={}, callback=function(data,status) {
5 $("#response").html($("#response").html() + ' '+ data.message);
6 });
7 });
8 });
9</script>
10<button class="btn btn-primary" id="hello_btn" type="button">
11 Execute!
12</button>
13<br/>
14<div class="well" id="response">
15</div>
The code block above consists of a JavaScript section, using a jQuery button click event inside a document ready block. When the button is clicked, we will perform an ajax call using on of our own wrappers, which will communicate the response with the content already in the bootstrap well.
Finally, let’s test the page. Make sure you’re logged into OPNsense, then navigate to https://<host>/ui/samples/helloworld.
Tip
ajaxGet() performs a http get, ajaxCall() implements a http post.
Note
In case your page doesn’t refresh correctly, it might be stuck in your cache. This can be solved by flushing all items in it with:
rm /usr/local/opnsense/mvc/app/cache/*.php