рдирд╛рдо рд╕реНрдерд╛рди рдореЗрдВ HMVC

рд╣рд╛рд▓ рд╣реА рдореЗрдВ, рдПрдорд╡реАрд╕реА рдбрд┐рдЬрд╛рдЗрди рдпреЛрдЬрдирд╛ рдХреЗ рдмрд╛рд░реЗ рдореЗрдВ рдмрд╣реБрдд рдХреБрдЫ рдХрд╣рд╛ рдЧрдпрд╛ рд╣реИ, рд▓рдЧрднрдЧ рд╕рднреА рд▓реЛрдХрдкреНрд░рд┐рдп PHP рдлреНрд░реЗрдорд╡рд░реНрдХ рдиреЗ рдЗрд╕ рдпреЛрдЬрдирд╛ рдореЗрдВ рд▓рдВрдмреЗ рд╕рдордп рддрдХ рд╕реНрд╡рд┐рдЪ рдХрд┐рдпрд╛ рд╣реИред рдХреЛрд╣рди рдХреЗ рд▓рд┐рдП , рд╕рдВрд╕реНрдХрд░рдг 3 рд╕реЗ рд╢реБрд░реВ рд╣реЛрдХрд░, рдПрдХ рд╢реНрд░реЗрдгреАрдмрджреНрдз MVC рдпреЛрдЬрдирд╛ рд▓рд╛рдЧреВ рдХреА рдЧрдИ рд╣реИ - HMVC ред HMVC рдХреЗ рдлрд╛рдпрджреЗ рд╕рднреА рдХреЗ рд▓рд┐рдП рд╕реНрдкрд╖реНрдЯ рд╣реИрдВ рдФрд░ рд╣рдо рдпрд╣рд╛рдВ рдЙрдирдХреА рдЪрд░реНрдЪрд╛ рдирд╣реАрдВ рдХрд░реЗрдВрдЧреЗред

рдореИрдВ, рдПрдХ "рдкреЗрд╢реЗрд╡рд░ рдкреНрд░реЛрдЧреНрд░рд╛рдорд░" рдХреЗ рд░реВрдк рдореЗрдВ "рд╢реМрдХрд┐рдпрд╛ рдкреНрд░реЛрдЧреНрд░рд╛рдорд░" рдХреА рд╕реНрдерд┐рддрд┐ рдХреЛ рдмрджрд▓рдирд╛ рдЪрд╛рд╣рддрд╛ рд╣реВрдВ, рдореИрдВрдиреЗ рдлреИрд╕рд▓рд╛ рдХрд┐рдпрд╛ рдХрд┐ рдореИрдВрдиреЗ рдкрд╣рд▓реЗ рд╕реЗ рд╣реА рдкреАрдПрдЪрдкреА рдХрд╛ рдкрд░реНрдпрд╛рдкреНрдд рдЕрдзреНрдпрдпрди рдХрд┐рдпрд╛ рд╣реИ рдФрд░ рдпрд╣ рдлреНрд░реЗрдорд╡рд░реНрдХ рдХреЗ рд╕рд╛рде рдХрд╛рдо рдХрд░рдирд╛ рд╢реБрд░реВ рдХрд░рдиреЗ рдХрд╛ рд╕рдордп рдерд╛, рдореЗрд░реА рдмрд╣реБрдд рдкрд╣рд▓реЗ рдлреНрд░реЗрдорд╡рд░реНрдХ рдХрд╛ рд╡рд┐рдХрд▓реНрдк рдХреЛрд╣рдирд╛ рдкрд░ рдЧрд┐рд░ рдЧрдпрд╛, рдХреНрдпреЛрдВрдХрд┐ рдпрд╣ HMVC рдХреЛ рд▓рд╛рдЧреВ рдХрд░рддрд╛ рд╣реИ, рдФрд░ рд╕рд╛рдорд╛рдиреНрдп рддреМрд░ рдкрд░, рдЗрд╕рдХреА рд╕рд╛рджрдЧреА рдХреЗ рд▓рд┐рдП рдЗрд╕рдХреА рдмрд╣реБрдд рдкреНрд░рд╢рдВрд╕рд╛ рдХреА рдЬрд╛рддреА рд╣реИред

рд╡рд╛рд╕реНрддрд╡ рдореЗрдВ, рд╕рдм рдХреБрдЫ рд╕рд░рд▓ рдирд╣реАрдВ рдирд┐рдХрд▓рд╛, рд▓реЗрдХрд┐рди рдПрдорд╡реАрд╕реА рдЯреНрд░рд╛рдпрдбреНрд╕ рдХреЗ рдЕрд▓рдЧрд╛рд╡ рдХреА рдЕрдиреБрдкрд╕реНрдерд┐рддрд┐ рд╡рд┐рд╢реЗрд╖ рд░реВрдк рд╕реЗ рдХрд╖реНрдЯрдкреНрд░рдж рдереА, рдЯреНрд░рд╛рдпрдбреНрд╕ рдХреЛ рднрд╛рдЧреЛрдВ рдореЗрдВ рд╡рд┐рднрд╛рдЬрд┐рдд рдХрд┐рдпрд╛ рдЧрдпрд╛ рдФрд░ рд╡рд┐рднрд┐рдиреНрди рдлрд╝реЛрд▓реНрдбрд░реЛрдВ рдореЗрдВ рд░рдЦрд╛ рдЧрдпрд╛ред рдореЗрд░реА рд╕рдордЭ рдореЗрдВ, рдореЙрдбрд▓-рд╡реНрдпреВ-рдХрдВрдЯреНрд░реЛрд▓рд░ рдЯреНрд░рд╛рдпрдб рдХреЛ рдЕрдиреНрдп рдЯреНрд░рд╛рдпрдб рд╕реЗ рдЕрд▓рдЧ рдХрд┐рдпрд╛ рдЬрд╛рдирд╛ рдЪрд╛рд╣рд┐рдП, рдФрд░ рдПрдХ рд╣реА рдлрд╝реЛрд▓реНрдбрд░ рдореЗрдВ рд╣реЛрдирд╛ рдЪрд╛рд╣рд┐рдП, рдмреЗрд╢рдХ, рдЗрд╕реЗ рдХреЛрд╣рдирд╛ рдореЗрдВ рд▓рд╛рдЧреВ рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИ, рд▓реЗрдХрд┐рди рд╕рдм рдХреБрдЫ рдмрд╣реБрдд "рднрджреНрджрд╛" рдФрд░ рднреНрд░рд╛рдордХ рд▓рдЧреЗрдЧрд╛ред рдпрд╣ рдЕрднреА рднреА рд╕реНрдкрд╖реНрдЯ рдирд╣реАрдВ рдерд╛ рдХрд┐ PHP рдХреЗ рдРрд╕реЗ рдЕрджреНрднреБрдд рдлреАрдЪрд░ рдХрд╛ рдЙрдкрдпреЛрдЧ рдПрдХ рдирд╛рдорд╕реНрдерд╛рди рдХреЗ рд░реВрдк рдореЗрдВ рдХреНрдпреЛрдВ рдирд╣реАрдВ рдХрд┐рдпрд╛ рдЧрдпрд╛ рд╣реИ? рдореИрдВрдиреЗ рдХрдИ рдФрд░ рдПрдЪрдПрдорд╡реАрд╕реА рд░реВрдкрд░реЗрдЦрд╛рдУрдВ рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ рджреЗрдЦрд╛, рд▓реЗрдХрд┐рди рдХреЗрд╡рд▓ рдПрдХ "рдЖрддреНрдорд╛ рдиреАрдЪреЗ рдЭреВрда рдирд╣реАрдВ рдмреЛрд▓рддреА"ред рддрдм рдореИрдВрдиреЗ рдЦреБрдж HMVC рдХреЛ рд▓рд╛рдЧреВ рдХрд░рдиреЗ рдХрд╛ рдлреИрд╕рд▓рд╛ рдХрд┐рдпрд╛ред

рддреБрд░рдВрдд рдХрдИ рдирд┐рд░реНрджреЗрд╢ рд╕реНрдерд╛рдкрд┐рдд:


рдлрд┐рд░ рдЙрд╕рдиреЗ рдХреБрдЫ рдФрд░ рдЬреЛрдбрд╝рд╛:


рдирддреАрдЬрддрди, рд╣рдореЗрдВ рдПрдХ рдЫреЛрдЯрд╛ рдПрдЪрдПрдорд╡реАрд╕реА рдврд╛рдВрдЪрд╛ рдорд┐рд▓рд╛, рдореЙрдбрд▓ рдПрдХ рднреВрдорд┐рдХрд╛ рдирд╣реАрдВ рдирд┐рднрд╛рддрд╛ рд╣реИ, рдЗрд╕рд▓рд┐рдП рдореИрдВ рдХреЛрдб рдХреЛ рдЙрджреНрдзреГрдд рдирд╣реАрдВ рдХрд░рддрд╛, рдореИрдВ рдХреЗрд╡рд▓ рдпрд╣ рдХрд╣ рд╕рдХрддрд╛ рд╣реВрдВ рдХрд┐ рдореИрдВ рдПрдХ рд╕реНрдерд┐рд░ рд╡рд░реНрдЧ рдХреЗ рд▓рд┐рдП рдЗрдЪреНрдЫреБрдХ рд╣реВрдВред

рддреЛ, рдкрд╣рд▓реЗ рдСрдЯреЛрд▓реИрдб рд╡рд░реНрдЧ:
рдкрддрд╛ рдЪрд▓рддрд╛ рд╣реИ
class Autoload { static $dirs=array(); //      ! static function add( $dir ){ if( !in_array($dir,self::$dirs) ){ self::$dirs[]=$dir; } } static function findClass( $className ){ $path=str_replace('\\','/',$className).'.php'; foreach( self::$dirs as $dir ){ $file=$dir.$path; if( is_file($file) ){ return $file; } } return false; } static function loadClass( $className ){ if( $file=self::findClass( $className ) ){ require $file; } } } spl_autoload_register( '\\Autoload::loadClass' ); Autoload::add( __DIR__.'/'); 


NameSpaceAdapter рд╡рд░реНрдЧ:
рдкрддрд╛ рдЪрд▓рддрд╛ рд╣реИ
 namespace lib; class NameSpaceAdapter { public function getDirectory(){ return dirname( \Autoload::findClass( get_class($this) ) ).'/'; } public function getNameSpace(){ $c=trim(str_replace('\\',' ',get_class($this))); $path=explode(' ',$c); if( count($path) ){ array_pop($path); return ('\\'.implode('\\',$path)); } return null; } } 


рдирд┐рдпрдВрддреНрд░рдХ рд╡рд░реНрдЧ:
рдкрддрд╛ рдЪрд▓рддрд╛ рд╣реИ
 namespace lib; class Controller extends NameSpaceAdapter { protected $view_data=array(); protected $params=array(); protected $action=null; protected $uri=null; public function __construct( $a_uri=null , $controller_uri=null ){ $this->uri=$controller_uri; if( is_array($a_uri) && count($a_uri) ){ $action=$a_uri[0]; if( is_numeric($action) ){ $this->action='show'; $this->params=$a_uri; }else{ $this->action=array_shift($a_uri); $this->params=$a_uri; } }else{ $this->action='index'; } $this->action(); } public function action( $action=null, array $params=null){ if( $action ) $this->action=$action; if( $params ) $this->params=$params; $method='action_'.$this->action; if( !\method_exists($this,$method) ){ $this->action=null; //     $method='action404'; } if( $this->before_action() ){ $this->$method(); }else{ $this->setError('e  '); } return $this; } protected function before_action(){ return true; } //      -  protected function before_view(){ } //     -  protected function action404(){ $this->setError('  '); } protected function action_index(){ throw new \Exception('<pre> index  '); } protected function action_show(){ throw new \Exception('<pre> show  '); } protected function setError( $message ){ $this->action='error'; //    error.php $this->view_data['title']=''; $this->view_data['message']=$message; } public function render(){ $this->before_view(); //  View,      ,  NameSpaceAdapter $viewClass=$this->getNameSpace().'\\View'; $view=new $viewClass(); if( is_array($this->view_data) && count($this->view_data) ){ foreach( $this->view_data as $key=>$value ){ $view->set($key,$value); } } if( $this->action ) $view->set('action',$this->action); $view->set('uri',('/'.$this->uri)); if( defined('ADMIN') ){ // admin_action.php if( $this->action ) $view->set('admin_action','admin_'.$this->action); } return $view->render(); } public function __toString(){ return $this->render(); } } 


рдореБрдЦреНрдп рд╡рд░реНрдЧ - рдирд┐рдпрдВрддреНрд░рдХ рд╡рд┐рд╕реНрддрд╛рд░:
рдкрддрд╛ рдЪрд▓рддрд╛ рд╣реИ
 namespace lib\Controller; class Main extends \lib\Controller { protected $controllers=array(); //array( 'uri'=>array( 'title'=>'','class'=>''),... protected $content_controller; //   protected $auth_class='\\lib\\auth\\BasicAdmin'; // ,   function __construct(){ $this->checkAdmin(); $this->init(); $a_uri=$this->read_URI(); $this->call_controller($a_uri); } protected function read_URI(){ $arr=explode('?',$_SERVER["REQUEST_URI"]); $uri=$arr[0]; $dirt_uri=explode('/',$uri); $a_uri=array(); foreach( $dirt_uri as $i){ if( !empty($i) ) $a_uri[]=$i; } return $a_uri; } protected function call_controller($a_uri){ /*       action,  action ,           ,  ,  ,      $this->content_controller      ,    action404   $a_uri ,       ,   index */ //   , ..       action $this->action=null; $this->params=null; if( is_array($a_uri) && count($a_uri) ){ $method='action_'.$a_uri[0]; //    ,     action_show if( method_exists($this,$method) || is_numeric($a_uri[0]) ){ parent::__construct($a_uri); }else{ //  ,   $controller_uri=array_shift( $a_uri ); if( isset($this->controllers[ $controller_uri ]) ){ $this->content_controller=new $this->controllers[ $controller_uri ]['class']( $a_uri , $controller_uri ); }else{ $this->action404(); } } }else{ parent::__construct(); //   action_index } } protected function checkAdmin(){ $auth=new $this->auth_class(); if( $auth->login() ){ define('ADMIN',true); $this->prepareForAdmin(); //     } } public function render(){ /*     View,    AJAX,         AJAX    content- */ if( defined('AJAX') ){ if( $this->content_controller ){ return $this->content_controller->render(); }else{ return ' -   '; } }else{ if( $this->content_controller ) $this->view_data['content']=$this->content_controller->render(); //$this->view_data['controllers']=$this->controllers; return parent::render(); } } public function action_ajax(){ /*   uri /ajax/...   AJAX */ define('AJAX',true); $this->call_controller( $this->params); //   } public function action_login(){/*        login.php */} public function action_logout(){ $auth=new $this->auth_class(); $auth->logout(); $this->action('index'); } public function init(){ /*   */} protected function prepareForAdmin(){/*     */} } 


рджреЗрдЦреЗрдВ рд╡рд░реНрдЧ:
рдкрддрд╛ рдЪрд▓рддрд╛ рд╣реИ
 namespace lib; class View extends NameSpaceAdapter { static $public_uri; //  ,   ,     static $scripts=array(); //   ,    static $styles=array(); //   ,    static $global_data=array(); //   protected $data=array(); //  ,     protected $templates_dir='templates'; //    ,    protected $template='template.php'; //     ,    action='template' static function render_styles(){ /*   ,         \View::render_styles() */ $html=''; if( is_array(static::$styles) && count(static::$styles) ){ foreach( static::$styles as $style ){ $html.='<link href="'.static::$public_uri.'styles/'.$style.'" rel="stylesheet" type="text/css" />'."\n"; } } return $html; } static function render_scripts(){ $html=''; if( is_array(static::$scripts) && count(static::$scripts) ){ foreach( static::$scripts as $script ){ $html.='<script language="javascript" src="'.static::$public_uri.'scripts/'.$script.'"></script>'."\n"; } } return $html; } protected function getActionFile(){ $dir=$this->getDirectory().$this->templates_dir.'/'; if( defined('ADMIN') && isset($this->data['admin_action']) ){ //  admin_action.php $file=$dir.$this->data['admin_action'].'.php'; if( !file_exists($file) ) $file=$dir.$this->data['action'].'.php'; }elseif( isset($this->data['action']) ){ $file=$dir.$this->data['action'].'.php'; } if( $file && file_exists($file)){ return $file; }else{ return null; } } public function set( $name, $value=null ){ if( is_array($name) ){ foreach( $name as $key=>$value ){ $this->data[$key]=$value; } }else{ $this->data[$name]=$value; } return $this; } public function render(){ /*           (templates),       template.php,  admin_template.php    View::$global_data -    */ $dir=$this->getDirectory().$this->templates_dir.'/'; if( defined('ADMIN') ){ $template=$dir.'admin_'.$this->template; //   admin_template.php if( !file_exists($template) ) $template=$dir.$this->template; }else{ $template=$dir.$this->template; } extract( static::$global_data ); extract( $this->data, EXTR_OVERWRITE ); $public_uri=static::$public_uri; ob_start(); require ($template); return ob_get_clean(); } static function microRender( $template, $data ){ /*        ,   ""  template.php */ $dir=dirname( \Autoload::findClass(get_called_class()) ); $file=$dir.'/templates/'.$template.'.php'; if( file_exists($file) ){ if( is_array($data) ) extract($data); ob_start(); require ($file); return ob_get_clean(); }else{ return "     $file<br>"; } } public function __toString(){ return $this->view->render(); } } 



рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдХрд╛рд░реНрдпрд╛рдиреНрд╡рдпрди рдХрд╛ рдПрдХ рдмрд╣реБрдд рд╣реА рд╕рдВрдХреНрд╖рд┐рдкреНрдд рдЙрджрд╛рд╣рд░рдг рд╣реИ

рдореБрдЦреНрдп рдирд┐рдпрдВрддреНрд░рдХ рдХрд╛ рдХрд╛рд░реНрдпрд╛рдиреНрд╡рдпрди:
рдкрддрд╛ рдЪрд▓рддрд╛ рд╣реИ
 namespace main; class Controller extends \lib\Controller\Main { protected $controllers=array( 'pages'=>array( 'title'=>'', 'class'=>'\\pages\\Controller' ) ); protected function action_index(){ $static_page=new \static_pages\Controller(array('get_page','index')); $this->view_data['content']=$static_page->render(); } protected function action_about(){ $static_page=new \static_pages\Controller(array('get_page','about')); $this->view_data['content']=$static_page->render(); } protected function action_contacts(){ $static_page=new \static_pages\Controller(array('get_page','contacts')); $this->view_data['content']=$static_page->render(); } protected function prepareForAdmin(){ if( defined('ADMIN') ){ //    ,     uri   $this->controllers['banners']=array( 'title'=>'', 'class'=>'\\banners\\Controller' ); $this->controllers['static_pages']=array( 'title'=>' ', 'class'=>'\\static_pages\\Controller' ); } return true; } protected function before_view(){ if( !defined('AJAX') ){ /*          View */ } } } 


рдореБрдЦреНрдп рдирд┐рдпрдВрддреНрд░рдХ рдХрд╛ рджреГрд╢реНрдп, рд╢реЗрд╖ рддреНрд░реИрдорд╛рд╕рд┐рдХ рдХрд╛ рджреГрд╢реНрдп рд▓рдЧрднрдЧ рд╕рдорд╛рди рджрд┐рдЦрддрд╛ рд╣реИ
рдкрддрд╛ рдЪрд▓рддрд╛ рд╣реИ
 namespace main; class View extends \View { function __construct(){ static::$scripts['jquery']='jquery-1.9.0.js'; static::$styles['main']='main.css'; } } 


рдореБрдЦреНрдп рдЯреЗрдореНрдкреНрд▓реЗрдЯ рдЯреЗрдореНрдкрд▓реЗрдЯ рдХрд╛ рдЙрджрд╛рд╣рд░рдг ред рдЬрд┐рд╕реЗ рдирд┐рдпрдВрддреНрд░рдХ рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИ
рдкрддрд╛ рдЪрд▓рддрд╛ рд╣реИ
 <div class='pages'> <?php if( $file=$this->getActionFile() ){ require $file; }else{ echo " "; } ?> </div> 



рджрд┐рдП рдЧрдП рдХреЛрдб "рдкреНрд░реЛрдЬреЗрдХреНрдЯ рдЗрди рдж рдЗрд╡рдирд┐рдВрдЧреНрд╕" рдХрд╛ рд╣рд┐рд╕реНрд╕рд╛ рд╣реИ, рдЬреЛ рд╣рд╛рдмрд░рд╛ рд╕рдВрдкрд╛рджрдХ рдореЗрдВ рдереЛрдбрд╝рд╛ рд╕рд╛ рдХрдЯрд╛ рд╣реБрдЖ рд╣реИ, рдЗрд╕рд▓рд┐рдП рдХреБрдЫ рдХрд╛рдо рдирд╣реАрдВ рдХрд░ рд╕рдХрддрд╛ рд╣реИ, рдбреЗрдореЛ рдЕрдкрд▓реЛрдб рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рддреИрдпрд╛рд░ рдирд╣реАрдВ рд╣реИ - рдпрд╣ рдЕрднреА рддрдХ рдирд╣реАрдВ рд╣реИред

рддреЛ рдореБрдЭреЗ рд╡рд╣реА рдорд┐рд▓рд╛ рдЬреЛ рдореИрдВ рдЪрд╛рд╣рддрд╛ рдерд╛:

рд╕рдЪ рд╣реИ, рдХреЛрдИ рдореЙрдбрд▓ рдФрд░ рдХреИрд╢рд┐рдВрдЧ рдирд╣реАрдВ рд╣реИ, рдФрд░ рд╕рд╛рде рд╣реА, рдЖрдкрдХреЛ рд╕реБрд░рдХреНрд╖рд╛ рдХреЗ рдмрд╛рд░реЗ рдореЗрдВ рдмрд╣реБрдд рд╕рд╛рд╡рдзрд╛рдиреА рд╕реЗ рд╕реЛрдЪрдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реИред
рдореИрдВ рдХрдорд┐рдпреЛрдВ рдХреЗ рдмрд╛рд░реЗ рдореЗрдВ рдкреЗрд╢реЗрд╡рд░реЛрдВ рдХреА рд░рд╛рдп рд╕реБрдирдирд╛ рдЪрд╛рд╣реВрдВрдЧрд╛, рдФрд░ рд╢рд╛рдпрдж рдпрд╣ рдПрдХ рд╕рд╛рдЗрдХрд┐рд▓ рдХрд╛ рдЖрд╡рд┐рд╖реНрдХрд╛рд░ рдХрд░рдиреЗ рдХреЗ рд▓рд╛рдпрдХ рдирд╣реАрдВ рд╣реИред

Source: https://habr.com/ru/post/In170739/


All Articles