{"id":175621,"date":"2013-04-05T15:20:03","date_gmt":"2013-04-05T11:20:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=175621"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=175621","title":{"rendered":"<span class=\"post_title\">\u0423\u0434\u043e\u0431\u043d\u0430\u044f \u0438 \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 PHP<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u041f\u043e\u0434\u0445\u043e\u0434\u044f \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0440\u043e\u0443\u0442\u0438\u043d\u0433\u0430 \u043c\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u0441 \u0434\u0438\u043b\u0435\u043c\u043e\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0438\u043b\u0438 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c.<br \/>  \u041e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u0441\u0442\u0430\u043b \u0441\u0430\u043c \u0440\u043e\u0443\u0442\u0435\u0440, \u0430 \u0441\u0440\u0435\u0434\u0432\u043e\u043c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u044b.<br \/>  \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f:<\/p>\n<h6>YAML<\/h6>\n<p>  + \u0418\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u044b\u0439<br \/>   \u2014 \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f (\u0434\u043b\u044f \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438)<br \/>   \u2014 \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 (\u043c\u0438\u043d\u0443\u0441, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043b\u0438\u0446\u0435\u043d\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438)<\/p>\n<h6>ini<\/h6>\n<p>  + \u0411\u044b\u0441\u0442\u0440\u044b\u0439 \u043f\u0430\u0440\u0441\u0438\u043d\u0433 php<br \/>  + \u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u0438 \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0445<br \/>   \u2014 \u0416\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u0441\u0435-\u0436\u0435 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c<br \/>   \u2014 \u0414\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b\u0439<\/p>\n<p>  <a name=\"habracut\"><\/a><\/p>\n<h6>PHP-file<\/h6>\n<p>  \u0412\u043e\u0442 \u043c\u044b \u0438 \u043f\u043e\u0434\u043e\u0448\u043b\u0438 \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u0432\u0430\u0436\u043d\u043e\u043c\u0443. \u041d\u043e \u0432\u043e\u043f\u0440\u043e\u0441 \u0432\u0441\u0435 \u0436\u0435 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f. \u041a\u0430\u043a \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0435\u0433\u043e?  <\/p>\n<pre><code class=\"php\">$routes = array(); $routes['routeName'] = array( 'pattern' =&gt; '\/my\/home\/page', 'controller' =&gt; 'homepage', 'action' =&gt; 'show' ); <\/code><\/pre>\n<p>  \u0412\u0430\u0440\u0438\u0430\u043d\u0442? \u041d\u043e \u0432\u0435\u0434\u044c \u043d\u0435 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e. \u0418 \u043d\u0435 \u0443\u0434\u043e\u0431\u043d\u043e \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0441 \u0442\u0430\u043a\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430, \u0438 \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u044b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u044b.<br \/>  \u0414\u0430 \u0438 \u0432\u0434\u0440\u0443\u0433 \u043a\u0442\u043e-\u0442\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043d\u0430\u043f\u0438\u0448\u0435\u0442 \u043d\u0435 controller, a controIler. (\u041c\u0430\u043b\u0435\u043d\u044c\u043a\u0430\u044f l \u0438 \u0431\u043e\u043b\u044c\u0448\u0430\u044f I \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e). \u041d\u0443 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c. \u041d\u0443 \u0432\u0435\u0434\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c.<\/p>\n<p>  \u0418\u043b\u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b \u043d\u0435 \u0440\u0435\u0436\u0435:<\/p>\n<pre><code class=\"php\">$router = new Router();  $route = new Route('RouteName', array('controller' =&gt; 'mycontroller', 'action' =&gt; 'myaction'));  $router-&gt;addRoute($route); <\/code><\/pre>\n<p>  \u041d\u0430\u0445\u043e\u0434\u0438\u043c \u0444\u0430\u0439\u043b router.php \u0438 \u0431\u0435\u0440\u0435\u043c \u043e\u0442\u0442\u0443\u0434\u0430 $router. \u0423\u0434\u043e\u0431\u043d\u043e? \u042f \u0441\u043e\u043c\u043d\u0435\u0432\u0430\u044e\u0441\u044c.<\/p>\n<h4>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u041c\u043e\u044f \u0438\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043a\u043b\u0430\u0441\u0441\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0432 \u0441\u0442\u0438\u043b\u0435 PHP5.<\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440:  <\/p>\n<pre><code class=\"php\">&lt;?php  namespace \u0421onfig;  use Routing\\Route;  class Routes {     function welcome() { \/\/\u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0435\u0441\u0442\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430         $a = new Route;         $a-&gt;setPattern('\/')           -&gt;setController('hello')           -&gt;setAction('welcome')           -&gt;setFormat('html');                  return $a;     }      function main() {         $a = new Route;         $a-&gt;setPattern('\/home\/{user}')           -&gt;setController('controller')           -&gt;setAction('list')           -&gt;setFormat('html');                  return $a;     } } <\/code><\/pre>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\\Routing\\Route.php<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"php\">&lt;?php  namespace Routing;  class Route  {          private $pattern    = null,             $controller = null,             $action     = null,             $parameters = array(),             $regexp     = null,             $format     = 'html';          public function setPattern($pattern) {         $this-&gt;regexp = str_replace('\/', '\\\/', $pattern);         $this-&gt;regexp = preg_replace('\/\\{\\w+\\}\/', '(\\w+)', $this-&gt;regexp);         $this-&gt;regexp = &quot;\/^{$this-&gt;regexp}$\/i&quot;;                  $this-&gt;pattern = $pattern;         return $this;     }     public function getPattern() {         return $this-&gt;pattern;     }     public function setController($controller) {         $this-&gt;controller = $controller;         return $this;     }     public function getController() {         return $this-&gt;controller;     }     public function setAction($action) {         $this-&gt;action = $action;         return $this;     }     public function getAction() {         return $this-&gt;action;     }     public function addParameter($name, $value) {         $this-&gt;parameters[$name] = $value;         return $this;     }     public function getParameters() {         return $this-&gt;parameters;     }          public function getRegexp() {         return $this-&gt;regexp;     }          public function setFormat($format) {         $this-&gt;format = $format;         return $this;     }     public function getFormat() {         return $this-&gt;format;     }          public function isValid() {                  if(is_null($this-&gt;getPattern())          ||is_null($this-&gt;getController())          ||is_null($this-&gt;getAction())        ) return false;                  return true;     }      }   <\/code><\/pre>\n<\/p><\/div>\n<\/div>\n<p>  \u0414\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0437\u044f\u0442\u044b\u0445 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432 \u0441 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \\Routing\\Router<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"php\">&lt;?php  namespace Routing;  use \\Routing\\Exception\\Route as RouteException;  class Router {          protected $routes  = array(),               $address = null;          public function __construct($str){         $this-&gt;address = $str;     }          public function init(){         $routes = new \\Config\\Routes;                  $resRoute = null;         foreach(get_class_methods($routes) as $route) {             if(!is_callable(array($routes, $route))) {                 throw new RouteException('All routes must be public.');             }                          $route = call_user_func(array($routes, &quot;$route&quot;));                          if(!$route-&gt;isValid()) {                 throw new RouteException('Invalid route found.');             }                          $params = array();             if(preg_match($route-&gt;getRegexp(), $this-&gt;address, $params)) {                                  if(count($params) == 0) {                     $resRoute = $route;                     break;                 }                                  $input = array();                 preg_match('\/\\{(\\w+)\\}\/', $route-&gt;getPattern(), $input);                                  for($i=1; $i&lt; count($input); $i++) {                     $route-&gt;addParameter($input[$i], $params[$i]);                 }                 $resRoute = $route;                 break;             }         }                  if(is_null($resRoute)) {             throw new RouteException('404', 404);         }                  return $resRoute;     }      }  <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a\u043e\u0439:  <\/p>\n<ul>\n<li>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0430\u0434\u0440\u0435\u0441\u0443<\/li>\n<li>\u0418\u0449\u0435\u043c \u0448\u0430\u0431\u043b\u043e\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0434 \u0434\u0430\u043d\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441<\/li>\n<li>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 \u0448\u0430\u0431\u043b\u043e\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u0445 \u0432 \u043c\u0430\u0440\u0448\u0440\u0443\u0442<\/li>\n<li>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043c\u0430\u0440\u0448\u0440\u0443\u0442<\/li>\n<\/ul>\n<p>  \u0421\u0435\u0439\u0447\u0430\u0441 \u043c\u0435\u043d\u044f \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0430\u0440\u0441\u0438\u043d\u0433 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432 \u0432 regexp&#8217;\u044b.<\/p>\n<p>  \u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u0443\u0441\u043b\u044b\u0448\u0430\u0442\u044c \u0432\u0430\u0448\u0438 \u0438\u0434\u0435\u0438 \u0438 \u043a\u0440\u0438\u0442\u0438\u043a\u0443.<br \/>  \u0421\u043f\u0430\u0441\u0438\u0431\u043e.    \t \t\t   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/175621\/\"> http:\/\/habrahabr.ru\/post\/175621\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u041f\u043e\u0434\u0445\u043e\u0434\u044f \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0440\u043e\u0443\u0442\u0438\u043d\u0433\u0430 \u043c\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u0441 \u0434\u0438\u043b\u0435\u043c\u043e\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0438\u043b\u0438 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c.<br \/>  \u041e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u0441\u0442\u0430\u043b \u0441\u0430\u043c \u0440\u043e\u0443\u0442\u0435\u0440, \u0430 \u0441\u0440\u0435\u0434\u0432\u043e\u043c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u044b.<br \/>  \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f:<\/p>\n<h6>YAML<\/h6>\n<p>  + \u0418\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u044b\u0439<br \/>   \u2014 \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f (\u0434\u043b\u044f \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438)<br \/>   \u2014 \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 (\u043c\u0438\u043d\u0443\u0441, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043b\u0438\u0446\u0435\u043d\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438)<\/p>\n<h6>ini<\/h6>\n<p>  + \u0411\u044b\u0441\u0442\u0440\u044b\u0439 \u043f\u0430\u0440\u0441\u0438\u043d\u0433 php<br \/>  + \u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u0438 \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0445<br \/>   \u2014 \u0416\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u0441\u0435-\u0436\u0435 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c<br \/>   \u2014 \u0414\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b\u0439<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-175621","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/175621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=175621"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/175621\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=175621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=175621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=175621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}