Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
c60d3f3e
Commit
c60d3f3e
authored
Feb 06, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
extended example with a simple REST call
parent
1925af93
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
77 additions
and
20 deletions
+77
-20
IndexController.php
...c/app/controllers/OPNsense/Sample/Api/IndexController.php
+7
-1
PageController.php
...se/mvc/app/controllers/OPNsense/Sample/PageController.php
+33
-15
index.volt
src/opnsense/mvc/app/views/OPNsense/Sample/index.volt
+36
-3
footer.volt
src/opnsense/mvc/app/views/layout_partials/footer.volt
+0
-1
default.volt
src/opnsense/mvc/app/views/layouts/default.volt
+1
-0
No files found.
src/opnsense/mvc/app/controllers/OPNsense/Sample/Api/IndexController.php
View file @
c60d3f3e
...
...
@@ -41,6 +41,12 @@ class IndexController extends ApiControllerBase
*/
public
function
indexAction
()
{
return
array
(
"message"
=>
"test"
);
if
(
$this
->
request
->
hasPut
(
"message"
))
{
$message
=
$this
->
request
->
getPut
(
"message"
);
}
else
{
$message
=
" "
;
}
return
array
(
"message"
=>
"you send : "
.
$message
);
}
}
src/opnsense/mvc/app/controllers/OPNsense/Sample/PageController.php
View file @
c60d3f3e
...
...
@@ -64,6 +64,32 @@ class PageController extends ControllerBase
}
}
/**
* perform validation, forward to index on failure.
* @param $mdlSample Sample model
* @return bool validation status
*/
private
function
performFormValidation
(
$mdlSample
)
{
$validationOutput
=
$mdlSample
->
performValidation
();
if
(
$validationOutput
->
count
()
>
0
)
{
// forward to index including errors
$error_msgs
=
array
();
foreach
(
$validationOutput
as
$msg
)
{
$error_msgs
[]
=
array
(
"field"
=>
$msg
->
getField
(),
"msg"
=>
$msg
->
getMessage
());
}
// redirect to index
$this
->
dispatcher
->
forward
(
array
(
"action"
=>
"index"
,
"params"
=>
array
(
$error_msgs
)
));
return
false
;
}
return
true
;
}
/**
* controller for sample index page, defaults to http://<host>/sample/page
* @param array $error_msg error messages
...
...
@@ -102,8 +128,11 @@ class PageController extends ControllerBase
$this
->
updateModelWithPost
(
$mdlSample
);
if
(
$this
->
request
->
getPost
(
"form_action"
)
==
"add"
)
{
// implement addRow, append new model row and serialize to config
// implement addRow, append new model row and serialize to config
if form is valid
$mdlSample
->
childnodes
->
section
->
add
();
if
(
$this
->
performFormValidation
(
$mdlSample
)
==
false
)
{
return
false
;
}
$mdlSample
->
serializeToConfig
();
}
elseif
(
$this
->
request
->
getPost
(
"form_action"
)
==
"save"
)
{
// implement save, possible removing
...
...
@@ -120,23 +149,12 @@ class PageController extends ControllerBase
}
}
// save data to config
$validationOutput
=
$mdlSample
->
performValidation
();
if
(
$validationOutput
->
count
()
>
0
)
{
// forward to index including errors
$error_msgs
=
array
();
foreach
(
$validationOutput
as
$msg
)
{
$error_msgs
[]
=
array
(
"field"
=>
$msg
->
getField
(),
"msg"
=>
$msg
->
getMessage
());
}
// redirect to index
$this
->
dispatcher
->
forward
(
array
(
"action"
=>
"index"
,
"params"
=>
array
(
$error_msgs
)
));
// form validation
if
(
$this
->
performFormValidation
(
$mdlSample
)
==
false
)
{
return
false
;
}
// save data to config
$mdlSample
->
serializeToConfig
();
$cnf
=
Config
::
getInstance
();
$cnf
->
save
();
...
...
src/opnsense/mvc/app/views/OPNsense/Sample/index.volt
View file @
c60d3f3e
test sample index page
{{ title }}
{{ partial('layout_partials/footer') }}
<h1> OPNsense sample module </h1>
A simple input form for the "sample" model can be found <a href="page/">here </a><br/>
<hr/>
To perform a call to the api, press this button : <br/>
fill in a message : <input type="text" value="" id="msg"> </br>
<input type="button" id="restcall" value="do REST call!"/>
<br/>
API call result : <div id="msgid"></div>
<script type="text/javascript">
$( "#restcall" ).click( function() {
$.ajax({
type: "PUT",
url: "/api/sample/",
success: function(data){
$("#msgid").html( data.message );
},
data:{message:$("#msg").val()}
});
});
</script>
src/opnsense/mvc/app/views/layout_partials/footer.volt
deleted
100644 → 0
View file @
1925af93
//////footer/////
src/opnsense/mvc/app/views/layouts/default.volt
View file @
c60d3f3e
...
...
@@ -2,6 +2,7 @@
<html>
<head>
<title>
{{title|default("OPNsense") }}
</title>
<script
type=
"text/javascript"
src=
"/js/jquery-1.11.1.min.js"
></script>
</head>
<body>
{{ content() }}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment