Skip to content

Instantly share code, notes, and snippets.

@jjjjeeffff
Created May 16, 2009 00:09
Show Gist options
  • Save jjjjeeffff/112508 to your computer and use it in GitHub Desktop.
Save jjjjeeffff/112508 to your computer and use it in GitHub Desktop.
<?php
class URLConf
{
private $_conf = array();
private $_controller;
private $_method;
private $_args;
private $_default_controller;
private $_default_func;
function __construct($conf = array()) {
$this->_default_controller = 'index';
$this->_default_method = 'index';
$this->_conf = $conf;
}
public function resolveURL() {
$request = parse_url($_SERVER['REQUEST_URI']);
$request = array_filter(explode("/", $request['path']));
$request = implode('/', $request);
foreach ($this->_conf as $regex => $exec_str) {
$regex = '/'.str_replace("/", '\/', $regex).'/';
if (preg_match("$regex", $request, $matches)) {
$exec = explode(":", $exec_str);
$this->_args = array();
$args = ($exec[1] ? explode(",", $exec[1]) : array());
foreach ($args as $arg) {
$this->_args[$arg] = $matches[$arg];
}
$call = explode("/", $exec[0]);
$this->_controller = ($call[0] != '' ? $call[0] : $this->_default_controller);
$this->_method = ($call[1] != '' ? $call[1] : $this->_default_method);
return true;
}
}
return false;
}
public function setController($controller) {
$this->_controller = $controller;
}
public function setMethod($method) {
$this->_method = $method;
}
public function setArgs($args = array()) {
$this->_args = $args;
}
public function getDispatch() {
// my_big_method becomes myBigMethod
$method = ucwords(str_replace('_', ' ', $this->_method));
$method = explode(" ", $method);
$method[0] = strtolower($method[0]);
$method = implode("", $method);
// foocont becomes FoocontController
$controller = ucwords(strtolower($this->_controller)).'Controller';
return array(
'controller' => $controller,
'method' => $method,
'args' => $this->_args,
'raw_controller' => strtolower($this->_controller),
'raw_method' => strtolower($this->_method)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment