1. Go to this page and download the library: Download phery/phery library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
function pre_function_one($ajax_data, $callback_specific_data, $phery_instance)
{
// Trim the data, assuming every item is a string, and not an array of array
return array_map('trim', $ajax_data);
}
function pre_function_two($ajax_data, $callback_specific_data, $phery_instance)
{
// Remove empty
return array_filter($ajax_data);
}
function post_function($ajax_data, $callback_specific_data, $answer, $phery_instance)
{
Database::insert('table', $ajax_data); // key/value pairs for e.g.
// $answer can be a PheryResponse
if ($answer instanceof PheryResponse)
{
$answer->alert('Ive been post processed!');
}
return true;
}
Phery::instance()->callback(array(
'before' => array('pre_function_one', 'pre_function_two'),
'after' => array('post_function')
));
Phery::instance()
->set(array(
'delete' => 'process_delete'
))
->callback(array(
'before' => 'callback_function'
))
->data(1, 'string', array('named'=>'argument'), new myClass)
->process();
function callback_function($ajax_data, $parameters)
{
// Since we are not using named parameters, it needs to be accessed through ordinal indexes
if ($parameters[0] === 1)
{
$ajax_data['new_stuff'] = $parameters[3]->execute($parameters[1]);
}
return $ajax_data;
}
function process_delete($delete, $parameters)
{
$id = process($delete['new_stuff']);
$parameters[3]->clear();
Database::delete($id);
return PheryResponse::factory()->alert('Deleted');
}
function my_own_error_handler(){
Phery::respond(PheryResponse::factory()->exception('ERROR!'));
exit;
}
Phery::instance()->views(array(
'#container' => function($data, $params){
ob_start();
switch ($_GET['page']){
case 'home':
case 'about':
case 'services':
$phery = Phery::instance();
$phery->set(array('function' => array('class', 'funct')));
$phery->process(false);
// ...
// continue execution
$phery->set(array(
'function2' => array('class' => 'funct2')
))
->callback(array(
'post' => 'function'
))
->process();
// PHP 'exit' is called, from this point and beyond won't get executed unless it doesnt map to any AJAX calls
$i += 10;
function outside($ajax_data, $callback_data){
return PheryResponse::factory();
}
class classy {
function inside($ajax_data, $callback_data){
return PheryResponse::factory();
}
static function inside_static($ajax_data, $callback_data){
return PheryResponse::factory();
}
}
$class = new classy();
Phery::instance()->set(array(
'alias' => function(){
return PheryResponse::factory();
},
'outside' => 'outside',
'class' => array($class, 'inside'),
'class' => 'classy::inside_static',
'namespaced' => 'namespaced\function'
));
function func($ajax_data, $callback_data, $phery_instance){
// $ajax_data = data coming from browser, via AJAX
//
// $callback_data = can have anything you specify, plus additional information, like **submit_id** that
// comes automatically from the AJAX request, containing the ID of the calling DOM element, if has an id="" set
//
// $phery_instance = the current instance of Phery
//
return PheryResponse::factory(); // In most cases, you'll want to return a PheryResponse object
}
function func($data, $params, $phery)
{
$user = ORM::factory('user', $data['id'])
->values($data)
->update();
return
PheryResponse::factory('#name')
/* Set the HTML of the #name */
->html('<p>'.$user->name.'</p>')
->show('fast')
/* Call a jQuery plugin, same as $('.slider').slider(...) */
->jquery('.slider')
->slider(array(
'value' => 60,
'orientation' => "horizontal",
'range' => "min",
'animate' => true
))
/* Creating elements on-the-fly, same $('<p/>') */
->jquery('<p/>', array('class' => 'attention', 'text' => 'Attention!'))
->appendTo('header .messages')
/* Nesting responses */
->add(PheryResponse::factory('p')->effect('highlight'))
/* Apply to both p that was created and existing p's on the page */
->addClass('changed');
}
html
<a data-phery-args="<?=Phery::args(array('id' => 1));
js
phery.remotes([
['function',{args:1}], //same as phery.remote('function', {args: 1});
['function2'], // same as phery.remote('function2');
['function3', null, {target:'/target'}] // same as phery.remote('function3', null, {target: '/target'});
]);