$lego = new MyLego('my_name'); // - // . run() GET- : $lego->run(); echo $lego->getOutput(); // Lego-
// http://example.ru/?my_name[act]=index&my_name[0]=1&my_name[1]=abc
abstract class Lego{ private $name; private $output; public function __construct($name = false){ // , if(!$name) $name = get_class($this); $this->name = $name; } public function getName(){ return $this->name; } public function getOutput(){ return $this->output; } public function run(){ $action = $this->getAction(); // , , action_ $method_name = 'action_'.$act; if(method_exists($this, $method_name)){ // , GET !!! // , , $this->output $this->output = call_user_func_array( array( $this, $method_name ), $this->getActionParams($action)); } else $this->output = "method {$action} does't exists"; // AJAX. ajax={_} if($this->_get("ajax") == $this->getName()){ echo $this->output; // die; // . } } // ( my_name[act]=index); public function getAction(){ $lego_params = $this->getLegoParams(); if(!is_array($lego_params)) return; if(isset($lego_params["act"])) return $lego_params["act"]; return "default"; // , GET } // , Lego public function getLegoParams($lego_name = false){ if(!$lego_name) $lego_name = $this->getName(); return $this->_get($lego_name, array()); } // ( &my_name[0]=1&my_name[1]=abc); public function getActionParams($action){ $lego_params = $this->getLegoParams(); if(!isset($lego_params[$action])) return array(); if(!is_array($lego_params[$action])) return array(); return $lego_params[$action]; } // , GET static public function _get($key_name, $default_value = false){ return self::__get_from_array($_GET, $key_name, $default_value); } // , , static private function __get_from_array($array, $key_name, $default_value = false){ if(!isset($array[$key_name])) return $default_value; return $array[$key_name]; } }
class FotoLego extends Lego{ // http://example.ru/?{lego_name}[act]=allfotos public function action_allfotos(){ $out = " "; return $out; } // http://example.ru/?{lego_name}[act]=onefoto&{lego_name}[0]={id_} public function action_onefoto($foto_id){ $out = " id=$foto_id"; return $out; } // http://example.ru/?{lego_name}[act]=bestfotos public function action_bestfotos(){ $out = " "; return $out; } }
class SomeLego extends Lego{ .... .... public function action_someMethod(){ // Lego- $lego = new SomeSublego("sublego_name1"); $lego->run(); // return $lego->getOutput(); // } public function action_someMethodElse(){ // Lego- $lego = new SomeSublegoElse("sublegoelse_name1"); $lego->run(); // , , Lego : Smarty::assign("content", $lego->getOutput()); return Smarty::fetch("some_template.tpl"); // } .... .... }
// : // http://example.ru/?LegoSite=fotos&ajax={______} // Lego::run(),
class LegoSite extends Lego{ // public function action_default(){ //? Default , // : $lego = new LegoAuth(); $lego->run(); // Smarty::assign("auth_block", $lego->getOutput()); // LegoAuth // : $lego = new LegoHotNews(); $lego->run(); // Smarty::assign("hotnews_block", $lego->getOutput()); // LegoHotNews // , , : $lego = new LegoArticles(); $lego->run(); // Smarty::assign("articles_block", $lego->getOutput()); // LegoArticles // -: Smarty::fetch("body.tpl"); // } // " ". public function action_about(){ return Smarty::fetch("about_site.tpl"); // } }
include ".common/autoload.php"; // $lego = new LegoSite(); // $lego->run(); // echo $lego->getOutput();// !
// , jQuery.fn.lego.load = function(lego_name, url, data, nocache){ jQuery.fn.lego.lastLoadedUrl = urldecode(url); jQuery.fn.lego.loadedUrls[lego_name] = urldecode(url); var lego = $("div.lego[name="+lego_name+"]"); lego.addClass("loading"); var pellicle = $("<div>"); // , pellicle.addClass('pellicle'); $(".lego[name="+lego_name+"]").prepend(pellicle); var no_ajax_url = jQuery.fn.lego.getNoAjaxUrl(url); // - , if($(".lego[name="+lego_name+"]").length != 1){ document.location = no_ajax_url; return; } location.hash = url; // // var from_cache = LegoCache.get(lego_name, url); if(from_cache && data == null && !nocache){ // $(".lego[name="+lego_name+"]").replaceWith(from_cache); return; } $.ajax({ type: data == null ? "GET" : "POST", url: url, data: data, success: function(received){ $().lego.log(", : "+received.length+" "+lego_name+"..."); if($(received).hasClass('lego')){ $(".lego[name="+lego_name+"]").replaceWith(received); // LegoCache.put(lego_name, url, received); } else{ $().lego.log(lego_name+": Lego: "+url); document.location = no_ajax_url; } }, error: function(x){ $().lego.log(" url: "+url); } }); } jQuery.fn.lego.ajaxEnable = function(selector){ jQuery.fn.lego.startProcessHash(); if(!selector) selector = ""; // $(selector+":not(.noajax) a:not(.noajax)").live("click.myEvent", function(e){ var href = $(e.currentTarget).attr("href"); // if(href.match(/^(http(s)?:\/\/)|(javascript)/i)) return true; var name = $(e.currentTarget).lego().attr("name"); var legotarget = $(e.currentTarget).attr("legotarget"); if(typeof legotarget == "undefined") legotarget = name; var ajax_url = jQuery.fn.lego.getAjaxUrl(href, legotarget); jQuery.fn.lego.load(legotarget, ajax_url); return false; }); // $("form:not(.noajax)").livequery("submit", function(e){ var name = $(this).lego().attr("name"); var legotarget = $(this).attr("legotarget"); if(typeof legotarget == "undefined") legotarget = name; jQuery.fn.lego.load(legotarget, $().lego.getAjaxUrl($(this).attr("action"), legotarget), $(this).serialize()); return false; }); } // var LegoCache = { cache: {}, put: function(lego_name, url, data){ this.cache[lego_name+url] = data; }, get: function(lego_name, url, data){ if(typeof this.cache[lego_name+url] != 'undefined'){ var ret = $(this.cache[lego_name+url]); var reload_block = $("<a href='javascript:void(0)' onclick='jQuery.fn.lego.reload(this)' />"); reload_block.html(" , "); reload_block.addClass('reload_block'); try{ ret.prepend(reload_block); }catch(e){} return ret; } } }
Source: https://habr.com/ru/post/109481/