Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ function delete_example() {
})->name('hello')->conditions(array('name' => '\w+'));


/*** ROUTES WITH OPTIONAL SEGMENTS AND DEFAULT ARGUMENTS ***/

Slim::get('/archive/:year(/:month(/:day))', function ( $year, $month = 5, $day = 20 ) {
echo "<p>The date is: $month/$day/$year</p>";
});

/*** RUN SLIM ***/

Slim::run();
Expand Down
6 changes: 4 additions & 2 deletions slim/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,14 @@ private function getPutParameters() {
* Fetch HTTP request headers
*
* @author Kris Jordan <http://www.github.com/KrisJordan>
* @author Jud Stephenson <http://judstephenson.com/blog>
* @return array
*/
private function getHttpHeaders() {
$httpHeaders = array();
foreach ( array_keys($_SERVER) as $key ) {
if ( substr($key, 0, 5) === 'HTTP_' ) {
$httpHeaders[substr($key, 5)] = $_SERVER[$key];
if ( (substr($key, 0, 5) === 'HTTP_') || (substr($key, 0, 8) === 'PHP_AUTH') ) {
$httpHeaders[((substr($key, 0, 5) == 'HTTP_') ? substr($key, 5) : substr($key, 4))] = $_SERVER[$key];
}
}
return $httpHeaders;
Expand All @@ -268,6 +269,7 @@ private function getHttpHeaders() {
public function header( $name ) {
return isset($this->headers[$name]) ? $this->headers[$name] : null;
}


/**
* Check for HTTP request method override
Expand Down
203 changes: 144 additions & 59 deletions slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,119 @@ class Route {
* @param mixed $callable Anything that returns TRUE for is_callable()
*/
public function __construct( $pattern, $callable ) {
$this->pattern = ltrim($pattern, '/');
$this->callable = $callable;
$this->setPattern(str_replace(')', ')?', ltrim($pattern, '/')));
$this->setCallable($callable);
}

/***** ACCESSORS *****/

/**
* Get route pattern
*
* @return string
*/
public function getPattern() {
return $this->pattern;
}

/**
* Set route pattern
*
* @param string $pattern
* @return void
*/
public function setPattern( $pattern ) {
$this->pattern = $pattern;
}

/**
* Get route callable
*
* @return mixed
*/
public function getCallable() {
return $this->callable;
}

/**
* Set route callable
*
* @param mixed $callable
* @return void
*/
public function setCallable($callable) {
$this->callable = $callable;
}

/**
* Get route conditions
*
* @return array
*/
public function getConditions() {
return $this->conditions;
}

/**
* Set route conditions
*
* @param array $conditions
* @return void
*/
public function setConditions( $conditions ) {
$this->conditions = (array)$conditions;
}

/**
* Get route name
*
* @return string|null
*/
public function getName() {
return $this->name;
}

/**
* Set route name
*
* @param string $name
* @return void
*/
public function setName( $name ) {
$this->name = $name;
$this->getRouter()->cacheNamedRoute($name, $this);
}

/**
* Get route parameters
*
* @return array
*/
public function getParams() {
return $this->params;
}

/**
* Get router
*
* @return Router
*/
public function getRouter() {
return $this->router;
}

/**
* Set router
*
* @param Router $router
* @return void
*/
public function setRouter( Router $router ) {
$this->router = $router;
}

/***** ROUTE PARSING AND MATCHING *****/

/**
* Matches URI?
*
Expand All @@ -104,26 +213,23 @@ public function __construct( $pattern, $callable ) {
public function matches( $resourceUri ) {

//Extract URL params
preg_match_all('@:([\w]+)@', $this->pattern, $paramNames, PREG_PATTERN_ORDER);
preg_match_all('@:([\w]+)@', $this->getPattern(), $paramNames, PREG_PATTERN_ORDER);
$paramNames = $paramNames[0];

//Convert URL params into regex patterns, construct a regex for this route
$patternAsRegex = preg_replace_callback('@:[\w]+@', array($this, 'convertPatternToRegex'), $this->pattern);
if ( substr($this->pattern, -1) === '/' ) {
$patternAsRegex = preg_replace_callback('@:[\w]+@', array($this, 'convertPatternToRegex'), $this->getPattern());
if ( substr($this->getPattern(), -1) === '/' ) {
$patternAsRegex = $patternAsRegex . '?';
}
$patternAsRegex = '@^' . $patternAsRegex . '$@';

//Cache URL params' names and values if this route matches the current HTTP request
if ( preg_match($patternAsRegex, $resourceUri, $paramValues) ) {
//If route pattern has trailing slash and the resource URL does not have
//a trailing slash, throw a SlimRequestSlashException
if ( substr($this->pattern, -1) === '/' && substr($resourceUri, -1) !== '/' ) {
throw new SlimRequestSlashException();
}
array_shift($paramValues);
foreach ( $paramNames as $index => $value ) {
$this->params[substr($value, 1)] = urldecode($paramValues[$index]);
if ( isset($paramValues[substr($value, 1)]) ) {
$this->params[substr($value, 1)] = urldecode($paramValues[substr($value, 1)]);
}
}
return true;
} else {
Expand All @@ -141,76 +247,55 @@ public function matches( $resourceUri ) {
private function convertPatternToRegex( $matches ) {
$key = str_replace(':', '', $matches[0]);
if ( array_key_exists($key, $this->conditions) ) {
return '(' . $this->conditions[$key] . ')';
return '(?P<' . $key . '>' . $this->conditions[$key] . ')';
} else {
return '([a-zA-Z0-9_\-\.\!\~\*\\\'\(\)\:\@\&\=\$\+,%]+)';
return '(?P<' . $key . '>[a-zA-Z0-9_\-\.\!\~\*\\\'\(\)\:\@\&\=\$\+,%]+)';
}
}

/***** HELPERS *****/

/**
* Return the callable for this Route
*
* @return mixed
*/
public function callable() {
return $this->callable;
}

/**
* Return the parameter names and values for this Route
*
* @return array
*/
public function params() {
return $this->params;
}

/**
* Set this route's Router
*
* @param Router The router for this Route
* @return void
*/
public function setRouter( Router $router ) {
$this->router = $router;
}

/**
* Get the pattern for this Route
*
* @return string
*/
public function pattern() {
return $this->pattern;
}

/**
* Set this route's name
* Set route name (alias for Route::setName)
*
* @param string $name The name of the route
* @return Route
*/
public function name( $name = null ) {
if ( !is_null($name) ) {
$this->name = (string)$name;
$this->router->cacheNamedRoute($name, $this);
}
public function name( $name ) {
$this->setName($name);
return $this;
}

/**
* Set this route's conditions
* Set route conditions (alias for Route::setConditions)
*
* @param array $conditions An associative array of URL parameter conditions
* @return Route
*/
public function conditions( $conditions = null ) {
public function conditions( $conditions ) {
if ( is_array($conditions) ) {
$this->conditions = $conditions;
}
return $this;
}

/***** DISPATCHING *****/

/**
* Dispatch route
*
* @return bool
*/
public function dispatch() {
if ( substr($this->getPattern(), -1) === '/' && substr($this->getRouter()->getRequest()->resource, -1) !== '/' ) {
throw new SlimRequestSlashException();
}
if ( is_callable($this->getCallable()) ) {
call_user_func_array($this->getCallable(), array_values($this->getParams()));
return true;
}
return false;
}

}

?>
Loading