Skip to content

Commit

Permalink
Added Uri library for the View class
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Jul 20, 2015
1 parent e6e7ddc commit 7d90130
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
67 changes: 67 additions & 0 deletions src/Uri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php namespace Rougin\Slytherin;

/**
* Uri Class
*
* @package Slytherin
*/
class Uri
{
/**
* Render the specified view file to the browser
*
* @param string $index
* @param array $segment
*/
public static function get($index = 0)
{
$segments = explode('/', self::parseRequest());
unset($segments[0]);

if ($index == 0 || ! is_numeric($index)) {
return $segments;
}

return $segments[$index];
}

/**
* Parse the $_SERVER['REQUEST_URI']
* @return [type] [description]
*/
protected static function parseRequest()
{
if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'])) {
return '';
}

$uri = parse_url($_SERVER['REQUEST_URI']);
$query = isset($uri['query']) ? $uri['query'] : '';
$uri = isset($uri['path']) ? $uri['path'] : '';

$_SERVER['QUERY_STRING'] = $query;

if (isset($_SERVER['SCRIPT_NAME'][0])) {
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) {
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
} else if (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) {
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
}
}

if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0) {
$query = explode('?', $query, 2);
$uri = $query[0];

$_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
}

parse_str($_SERVER['QUERY_STRING'], $_GET);

if ($uri === '/' OR $uri === '') {
return '/';
}

return $uri;
}
}
10 changes: 6 additions & 4 deletions src/View.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Rougin\Slytherin;

use Rougin\Slytherin\Uri;
use Twig_Environment;
use Twig_Loader_Filesystem;

Expand All @@ -8,8 +9,8 @@
*
* @package Slytherin
*/
class View {

class View
{
/**
* Render the specified view file to the browser
*
Expand All @@ -19,10 +20,11 @@ class View {
public static function render($view, $data = array())
{
$loader = new Twig_Loader_Filesystem(APPPATH . 'views');
$session = new Session();
$twig = new Twig_Environment($loader);

$data['session'] = (isset($_SESSION)) ? $_SESSION : array();
$data['uriSegment'] = Uri::get();

return $twig->render($view . '.php', $data);
}

}

0 comments on commit 7d90130

Please sign in to comment.