-
Notifications
You must be signed in to change notification settings - Fork 5
Rest
corentin-begne edited this page Feb 12, 2018
·
11 revisions
To use the Rest functionality you need to include the class in the loader or active the API Library. See : full doc
<?
$loader->registerClasses([
//...
'Rest' => $config->application->rootDir.'vendor/v-cult/phalcon/src/Tools/Rest.php',
//...
], true);
Example :
View [sample/test.phtml]
See :
ManagerModel
<form class='action' preventDefault stopPropagation action-data='<?=json_encode([
'type' => 'submit',
'fn' => 'action',
'class' => 'TestSampleManager',
'name' => 'save'
])?>'>
<?=\Phalcon\Tag::hiddenField([
'id'
])>
<?=\Phalcon\Tag::textField('username'))>
<?=Phalcon\Tag::submitButton([
'Send'
])?>
</form>
TestSampleManager [js]
See :
ActionModel
ActionHelper
TestSampleManager.prototype.save = function(element){
var that = this;
this.action.sendForm(this.basePath+"save", new FormData($(element)[0]), complete);
function complete(result){
if(!result.success){
console.error(result.error);
return false;
}
console.log(result.data);
}
}
SampleController [php]
<?
function testAction(){
Phalcon\Tag::setDefault('id', time());
}
function saveAction(){
Rest::init(); // check request validity
Rest::checkParams(['id', 'username']); // check required params
try{
//...
Rest::renderSuccess();
} catch(Exception){
Rest::renderError($e->getMessage());
}
}