/** * 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; } }
/** * Assigns the template [View] as the request response. */ public function after() { if (Phery::is_ajax(true)) { $this->auto_render = false; } parent::after(); if ($this->auto_render === TRUE) { $this->response->body($this->template->render()); } }
public function after() { $this->ajax->config(array_replace($this->ajax_config, $this->ajax(), array('exit_allowed' => false, 'return' => true))); parent::after(); if (Phery::is_ajax(true)) { try { if (($response = $this->ajax->process()) !== false) { $this->response->headers(array('Content-Type' => 'application/json'))->body($response); } } catch (PheryException $exc) { Kohana::$log->add(Log::ERROR, $exc->getMessage()); $answer = PheryResponse::factory(); if ($exc->getCode() === Phery::ERROR_CSRF) { $answer->renew_csrf($this->ajax); } $this->response->headers(array('Content-Type' => 'application/json'))->body($answer); } } }
function action_index() { echo '<html>'; echo '<head>'; echo $this->ajax->csrf(); echo '</head><body>'; echo HTML::script('javascripts/jquery.js'); echo HTML::script(Route::get('phery.js')->uri()); echo Phery::link_to('link', 'remote'); echo '</body></html>'; }
/** * Determine if the last selector or the selector provided is an special * * @param string $type * @param string $selector * @return boolean */ protected function is_special_selector($type = null, $selector = null) { $selector = Phery::coalesce($selector, $this->last_selector); if ($selector && preg_match('/\\{([\\D]+)\\d+\\}/', $selector, $matches)) { if ($type === null) { return true; } return $matches[1] === $type; } return false; }
function pseudo_controller() { if (isset($_GET['page'])) { switch ($_GET['page']) { case 'home': $title = 'Home'; $html = <<<HTML <h1>Home</h1> <p>Welcome to our website</p> <p><img src="http://lipsum.lipsum.com/images/lorem.gif"></p> HTML; return array($title, $html); break; case 'about': $title = 'About Us'; $html = <<<HTML <h1>About us</h1> <p> \tLorem ipsum dolor sit amet, consectetur adipiscing elit. \tPraesent ligula ante, auctor id commodo eu. </p> HTML; return array($title, $html); break; case 'contact': $title = 'Contact Us'; $form = Phery::form_for('', 'form'); $html = <<<HTML <h1>Contact us</h1> <p>Use the form below to contact us</p> {$form} <p> \t<label>Name</label> \t<input name="name" type="text"> </p> <p> \t<label>Email</label> \t<input name="email" type="email"> </p> <p> \t<label>Message</label> \t<textarea name="message"></textarea> </p> <p> \t<input type="submit" value="Send"> </p> </form> HTML; return array($title, $html); break; case 'excluded': return array('Excluded', '<h1>This is always reached through a full page load</h1><p>Because of the exclude param when creating the view</p>'); break; case 'redirect': return array(true, true); break; default: return array('Not Found', '<h1>404 Not Found</h1><p>The requested url was not found</p>'); break; } } else { return array('Welcome', '<h1>Welcome!</h1>'); } }