Skip to content

Commit

Permalink
optional forSlashAtEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
attogram committed May 21, 2018
1 parent 00904af commit ac44115
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/
.idea/
11 changes: 8 additions & 3 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@
vars : <?php
echo !empty($vars)
? '<span class="box good vars">' . print_r($vars, true) . '</span>'
: '<span class="box empty">null</span>';
?> &nbsp;
: '<span class="box empty">null</span>'; ?> &nbsp;
_GET : <?php
echo !empty($_GET)
? '<span class="box good vars">' . print_r($_GET, true) . '</span>'
: '<span class="box empty">null</span>'; ?> &nbsp;
</p>
<hr />
<ol>
Expand All @@ -68,10 +71,12 @@
<li><a href="<?= $base; ?>/test/FOO/test/"><?= $base; ?>/test/FOO/test/</a></li>
<li><a href="<?= $base; ?>/test/FOO/test/BAR/"><?= $base; ?>/test/FOO/test/BAR/</a></li>
<li><a href="<?= $base; ?>/test/FOO/test/BAR/BAZ/"><?= $base; ?>/test/FOO/test/BAR/BAZ/</a></li>
<li><a href="<?= $base; ?>/test/FOO/test/BAR/BAZ/?bar=baz"><?= $base; ?>/test/FOO/test/BAR/BAZ/?bar=baz</a></li>
<li><a href="<?= $base; ?>?a=b&x=y"><?= $base; ?>?a=b&x=y</a></li>
<li><a href="<?= $base; ?>/404/"><?= $base; ?>/404/</a></li>
</ol>
<hr />
<?= $title; ?>
<?= $title; ?>
- <a target="_blank" href="https://github.com/attogram/router">github</a>
- <a target="_blank" href="https://packagist.org/packages/attogram/router">packagist</a>
- <a target="_blank" href="https://codeclimate.com/github/attogram/router">codeclimate</a>
Expand Down
26 changes: 13 additions & 13 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
*/
class Router
{
const VERSION = '0.0.11';
const VERSION = '0.1.0';

public $forceSlashAtEnd = false;

private $allow = [];
private $control = '';
private $controls = [];
private $routesExact = [];
private $routesVariable = [];
private $uriBase = '';
private $uriRelative = '';
private $uri = [];
private $uriCount = 0;
private $allow = [];
private $routesExact = [];
private $routesVariable = [];
private $controls = [];
private $control = '';
private $vars = [];


/**
* __construct
* Sets: ->uriBase, ->uriRelative, ->uri, and ->uriCount
* Redirects to proper URL if no slash found at end of current URL
* @return void
* - optionally forces slash at end of URL
*/
public function __construct()
{
Expand All @@ -35,7 +35,7 @@ public function __construct()
$this->uriBase = rtrim($this->uriBase, '/'); // remove trailing slash from base URI
$this->uri = $this->trimArray(explode('/', $this->uriRelative)); // make uri list
$this->uriCount = count($this->uri);
if (1 !== preg_match('#/$#', $this->uriRelative)) { // If relative URI has no slash at end
if ($this->forceSlashAtEnd && 1 !== preg_match('#/$#', $this->uriRelative)) { // no slash at end of URL?
$this->redirect($this->uriBase . $this->uriRelative . '/'); // Force trailing slash
}
}
Expand All @@ -45,7 +45,7 @@ public function __construct()
* @param string $route
* @param string $control
*/
public function allow(string $route, string $control)
public function allow($route, $control)
{
$this->allow[] = [
'route' => $this->trimArray(explode('/', $route)),
Expand Down Expand Up @@ -132,7 +132,7 @@ private function matchVariable()
* @param array $route
* @return void
*/
private function matchVariableVars($route)
private function matchVariableVars(array $route)
{
$this->vars = [];
foreach ($route as $arrayId => $dir) {
Expand Down Expand Up @@ -179,7 +179,7 @@ private function redirect($url)
* @param string $name
* @return mixed
*/
private function getServer(string $name)
private function getServer($name)
{
if (!empty($_SERVER[$name])) {
return $_SERVER[$name];
Expand Down

0 comments on commit ac44115

Please sign in to comment.