/** * Initialize phery by scanning the target class for response methods. * This eliminates the need for phery::instance->set( ... array of response methods ), for quick setup. * * An data-remote method defined on the client side should have a corresponding response method * with the same name prefixed by "ph_". Usage example: * * View: * <button data-remote="testmethod" >Call ph_testmethod in target class</button> * or equivalent * <?php echo phery::link_to('Call ajax_testmethod in target class', 'testmethod', array('tag' => 'button')); ?> * * Controller: * public function before() { * parent::before(); * Pheryajax::init($this); // <-- This init function looks for response method "phery_testmethod" in the controller itself ($this) * } * * public function phery_testmethod($data = NULL) { // <-- "ajax_testmethod" corresponding to client side "testmethod" * return PheryResponse::factory()->alert('Hello from ph_testmethod!'); * } * * @param $target_class where to look for methods that will be triggered by ajax calls * @param $phery_callbacks */ public static function init($target_class, $phery_callbacks = array()) { try { Phery::instance()->config(array('exceptions' => true, 'unobstructive' => array('thisone')))->callback($phery_callbacks)->set(self::get_response_methods($target_class))->process(); } catch (PheryException $exc) { echo PheryResponse::factory()->alert($exc->getMessage()); exit; } }
public function before() { $this->ajax_config = Kohana::$config->load('Phery')->as_array(); $this->ajax = Phery::instance(array_replace($this->ajax_config, $this->ajax())); View::bind_global('ajax', $this->ajax); if (Phery::is_ajax(true)) { $methods = get_class_methods(get_class($this)); foreach ($methods as $method) { if (preg_match('/^ajax_(?<action>[a-z0-9]+)_(?<function>[a-z0-9\\_]+)$/i', $method, $matches)) { if (!strcasecmp($matches['action'], $this->request->action())) { $this->ajax->set(array($matches['function'] => array($this, $method))); } } elseif (preg_match('/^ajax_(?<function>[a-z0-9\\_]+)$/i', $method, $matches)) { $this->ajax->set(array($matches['function'] => array($this, $method))); } } } parent::before(); }
/** * Generates just one instance. Useful to use in many included files. Chainable * * @param array $config Associative config array * * @see __construct() * @see config() * @static * @return Phery */ public static function instance(array $config = array()) { if (!self::$instance instanceof Phery) { self::$instance = new Phery($config); } else { if ($config) { self::$instance->config($config); } } return self::$instance; }