Просмотр поста

.
L!MP

~XeOn~, В .htaccess

<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteBase /
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule .* index.php [L,QSA]
</IfModule>


В index.php

<?php 

require dirname(__FILE__).'/protected/Fuuu.php';

Fuuu::init(array(
	//...
));

Fuuu::route('GET', '/hello/:name?', function() {
	echo 'HELLO ' . Fuuu::router()->get_param('name');
});

Fuuu::run();


Где то в дебрях:

namespace Fuuu\Http
{
	class Router
	{
		protected $routes = array();
		protected $params = array();
		
		/*  */
		
		function __construct()
		{
			//...
		}
		
		/*
		*
		* @access public
		* @param string $key
		* @param mixed $value
		* @return mixed
		*/
		
		function get_param($key, $value = null)
		{
			return (isset($this->params[ $key ])
				? $this->params[ $key ]
				: $value
			);
		}
		
		/*
		*
		* @access public
		* @param string $methods
		* @param mixed string|array $patterns
		* @param mixed closure|array $callable
		* @return object $this
		*/
		
		function register($methods, $patterns, $callable)
		{
			if (is_array($patterns)) {
				foreach ($patterns as $pattern) {
					$this->register($methods, $pattern, $callable);
				}
			} else {
				foreach (explode('|', $methods === '*' ? 'GET|POST|PUT|DELETE' : strtoupper($methods)) as $method) {
					$this->routes[ $method ][ $patterns ] = $callable;
				}
			}
			
			return $this;
		}
		
		/*
		*
		* @access protected
		* @return mixed
		*/
		
		function dispatch( /* Request $request */ )
		{
			$method = 'GET';
			$uri    = '/category/35/5';
			
			if ($routes = count($this->routes[ $method ]) ? $this->routes[ $method ] : array()) {
				if (isset( $routes[ $uri ])) {
					return $routes[ $uri ];
				}
				
				foreach ($routes as $pattern => $callable) {
					if ($pattern === '*' || $this->match($pattern, $uri)) {
						return $callable;
					}
				}
			}
			
			return false;
		}
		
		/*
		*
		* @access protected
		* @param string $pattern
		* @param string $uri
		* @return boolean
		*/
		
		protected function match($pattern, $uri)
		{
			$pieces = explode('/', $pattern);
			$end    = 0;
			
			while (list($key, $value) = each($pieces)) {
				if (substr($value, -1) === '?') {
					$pieces[ $key - 1 ] = $pieces[ $key - 1 ] . '(?:';
					++$end; 
				}
				
				if ($value === '*') {
					$pieces[ $key ] = '(.+)';
				} else
				if ($value[0] === ':') {
					if (preg_match('/\w+)(\(([^\/]*)\))?/', $value, $matches)) {
						$pieces[ $key ] = (isset($matches[3])
							? '(?P<' . $matches[1] . '>' . $matches[3] . ')'
							: '(?P<' . $matches[1] . '>[\w]++)'
						);
					}
				}
			}
			
			$regex  = implode('\/', $pieces);
			$regex .= str_repeat(')?', $end);
			
			// echo $regex . '<br>';
			
			if (preg_match('/^' . $regex . '\/?$/i', $uri, $matches)) {
				$this->params = array_slice(array_unique($matches), 1);
				
				return true;
			}
			
			return false;
		}
		
	}
	
}


И это всё заставляет при обращении по адресу http://site.ru/hello/OLOLO выдать скрипту страницу с надписью "HELLO OLOLO"