Skip to content

Commit

Permalink
reduce repetitive getDefaultNamespace() and controllerName() function…
Browse files Browse the repository at this point in the history
… call in Router
  • Loading branch information
samsonasik committed Jul 13, 2020
1 parent b313c48 commit d2c77e7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,13 @@ public function autoRoute(string $uri)
$this->params = $segments;
}

$defaultNameSpace = $this->collection->getDefaultNamespace();
$controllerName = $this->controllerName();
if ($this->collection->getHTTPVerb() !== 'cli')
{
$controller = '\\' . $this->collection->getDefaultNamespace();
$controller = '\\' . $defaultNameSpace;
$controller .= $this->directory ? str_replace('/', '\\', $this->directory) : '';
$controller .= $this->controllerName();
$controller .= $controllerName;
$controller = strtolower($controller);
$methodName = strtolower($this->methodName());

Expand All @@ -575,17 +577,17 @@ public function autoRoute(string $uri)
}

// Load the file so that it's available for CodeIgniter.
$file = APPPATH . 'Controllers/' . $this->directory . $this->controllerName() . '.php';
$file = APPPATH . 'Controllers/' . $this->directory . $controllerName . '.php';
if (is_file($file))
{
include_once $file;
}

// Ensure the controller stores the fully-qualified class name
// We have to check for a length over 1, since by default it will be '\'
if (strpos($this->controller, '\\') === false && strlen($this->collection->getDefaultNamespace()) > 1)
if (strpos($this->controller, '\\') === false && strlen($defaultNameSpace) > 1)
{
$this->controller = '\\' . ltrim(str_replace('/', '\\', $this->collection->getDefaultNamespace() . $this->directory . $this->controllerName()), '\\');
$this->controller = '\\' . ltrim(str_replace('/', '\\', $defaultNameSpace . $this->directory . $controllerName), '\\');
}
}

Expand Down

0 comments on commit d2c77e7

Please sign in to comment.