Using symfony2 routing component (outside of symfony2) -
Using symfony2 routing component (outside of symfony2) -
i've been searching , haven't found much useful information. i'm looking utilize symfony2 routing component (and yaml component) own personal framework haven't found documentation on basic setup. documentation on symfony site job of explaining how utilize component not much in way of standalone setup.
can recommend place start explain how setup individual symfony2 components?
i ended looking @ documentation on symfony site, source code on github (route.php, routecollection.php, routecompiler.php) , came own basic router.
just symfony, allow 4 arguments passed route class -> 1) pattern, 2) default controller/action, 3) regex requirements each variable in pattern, , 4) options - haven't coded yet it's not in requirements.
my main/front controller (index.php) talks static router class has access other route classes in lib\route namespace.
router::map compiles regex string (along default controller/action , variable names) gets passed compiledroutecollection used in router::_process match httprequest against. there, if there match in process method set controller/action/args based on values of matched compiledroute default controller/action. if there not preg_match utilize default controller/action/args request object. rest cake...
i hope finds useful , it's able save them time spent today working on this. cheers!
index.php
require_once 'config/config.php'; require_once 'lib/autoload.php'; lib\router::map( 'users_username_id', 'users/{username}/{id}', array('controller' => 'testcontroller', 'action' => 'users') ); lib\router::route(new lib\request()); router.php
namespace lib; class router { protected static $_controller; protected static $_action; protected static $_args; protected static $_compiledroutecollection; private static $_instance; private function __construct() { self::$_compiledroutecollection = new \lib\route\compiledroutecollection(); } public static function singleton() { if(!isset(self::$_instance)) { $classname = __class__; self::$_instance = new $classname; } homecoming self::$_instance; } public static function route(request $request, $resetproperties = false) { self::singleton(); self::_process($request,$resetproperties); $classname = '\\app\\controllers\\' . self::$_controller; if(class_exists($classname, true)) { self::$_controller = new $classname; if(is_callable(array(self::$_controller, self::$_action))) { if(!empty(self::$_args)) { call_user_func_array(array(self::$_controller, self::$_action), self::$_args); } else { call_user_func(array(self::$_controller, self::$_action)); } return; } } self::route(new \lib\request('error'),true); } public static function map($name, $pattern, array $defaults, array $requirements = array(), array $options = array()) { self::singleton(); $route = new \lib\route\route($pattern,$defaults,$requirements,$options); $compiledroute = $route->compile(); self::$_compiledroutecollection->add($name,$compiledroute); } private static function _process(request $request, $resetproperties = false) { $routes = array(); $routes = self::$_compiledroutecollection->routes; foreach($routes $route) { preg_match($route[0]['regex'], $request->getrequest(), $matches); if(count($matches)) { array_shift($matches); $args = array(); foreach($route[0]['variables'] $variable) { $args[$variable] = array_shift($matches); } self::$_controller = (!isset(self::$_controller) || $resetproperties) ? $route[0]['defaults']['controller'] : self::$_controller; self::$_action = (!isset(self::$_action) || $resetproperties) ? $route[0]['defaults']['action'] : self::$_action; self::$_args = (!isset(self::$_args) || $resetproperties) ? $args : self::$_args; return; } } self::$_controller = (!isset(self::$_controller) || $resetproperties) ? $request->getcontroller() : self::$_controller; self::$_action = (!isset(self::$_action) || $resetproperties) ? $request->getaction() : self::$_action; self::$_args = (!isset(self::$_args) || $resetproperties) ? $request->getargs() : self::$_args; } } request.php
namespace lib; class request { protected $_controller; protected $_action; protected $_args; protected $_request; public function __construct($urlpath = null) { $this->_request = $urlpath !== null ? $urlpath : $_server['request_uri']; $parts = explode('/', $urlpath !== null ? $urlpath : $_server['request_uri']); $parts = array_filter($parts); $this->_controller = (($c = array_shift($parts)) ? $c : 'index').'controller'; $this->_action = ($c = array_shift($parts)) ? $c : 'index'; $this->_args = (isset($parts[0])) ? $parts : array(); } public function getrequest() { homecoming $this->_request; } public function getcontroller() { homecoming $this->_controller; } public function getaction() { homecoming $this->_action; } public function getargs() { homecoming $this->_args; } } route.php
namespace lib\route; class route { private $_pattern; private $_defaults; private $_requirements; public $_options; public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array()) { $this->setpattern($pattern); $this->setdefaults($defaults); $this->setrequirements($requirements); $this->setoptions($options); } public function setpattern($pattern) { $this->_pattern = trim($pattern); if($this->_pattern[0] !== '/' || empty($this->_pattern)) { $this->_pattern = '/'.$this->_pattern; } } public function getpattern() { homecoming $this->_pattern; } public function setdefaults(array $defaults) { $this->_defaults = $defaults; } public function getdefaults() { homecoming $this->_defaults; } public function setrequirements(array $requirements) { $this->_requirements = array(); foreach($requirements $key => $value) { $this->_requirements[$key] = $this->_sanitizerequirement($key,$value); } } public function getrequirements() { homecoming $this->_requirements; } public function setoptions(array $options) { $this->_options = array_merge( array('compiler_class' => 'lib\\route\\routecompiler'), $options ); } public function getoptions() { homecoming $this->_options; } public function getoption($name) { homecoming isset($this->_options[$name]) ? $this->_options[$name] : null; } private function _sanitizerequirement($key, $regex) { if($regex[0] == '^') { $regex = substr($regex, 1); } if(substr($regex, -1) == '$') { $regex = substr($regex,0,-1); } homecoming $regex; } public function compile() { $classname = $this->getoption('compiler_class'); $routecompiler = new $classname; $compiledroute = array(); $compiledroute = $routecompiler->compile($this); homecoming $compiledroute; } } routecompiler.php
namespace lib\route; class routecompiler { //'#\/tellme\/users\/[\w\d_]+\/[\w\d_]+#' public function compile(route $route) { $pattern = $route->getpattern(); $requirements = $route->getrequirements(); $options = $route->getoptions(); $defaults = $route->getdefaults(); $len = strlen($pattern); $tokens = array(); $variables = array(); $pos = 0; $regex = '#'; preg_match_all('#.\{([\w\d_]+)\}#', $pattern, $matches, preg_offset_capture | preg_set_order); if(count($matches)) { foreach($matches $match) { if($text = substr($pattern, $pos, $match[0][1] - $pos)) { $regex .= str_replace('/', '\/', $text).'\/'; } if($var = $match[1][0]) { if(isset($requirements[$var])) { $regex .= '('.$requirements[$var].')\/'; } else { $regex .= '([\w\d_]+)\/'; } $variables[] = $match[1][0]; } $pos = $match[0][1] + strlen($match[0][0]); } $regex = rtrim($regex,'\/').'#'; } else { $regex .= str_replace('/', '\/', $pattern).'#'; } $tokens[] = array( 'regex' => $regex, 'variables' => $variables, 'defaults' => $defaults ); homecoming $tokens; } } compiledroutecollection.php
namespace lib\route; class compiledroutecollection { public $routes; public function __construct() { $this->routes = array(); } public function add($name, array $route) { $this->routes[$name] = $route; } } symfony2 routing components yaml
Comments
Post a Comment