Skip to content

Commit

Permalink
Fix #33 and a bug with default parameter values in anonymous routes
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonsalas committed Sep 6, 2018
1 parent cf53d35 commit ba459d1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
19 changes: 18 additions & 1 deletion src/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function getHooks($config = null)
*/
private static function preSystemHook($config)
{
define('LUTHIER_CI_VERSION', '1.0.1');
define('LUTHIER_CI_VERSION', '1.0.3');
define('LUTHIER_CI_DIR', __DIR__);

$isAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH'])
Expand Down Expand Up @@ -293,6 +293,10 @@ private static function preControllerHook(&$params, &$URI, &$class, &$method)

$route->params[$pcount]->value = $URI->segment($i+1);

if(is_callable($route->getAction()) && !empty($URI->segment($i+1))){
$params[$route->params[$pcount]->getName()] = $URI->segment($i+1);
}

// Removing "sticky" route parameters
if(substr($route->params[$pcount]->getName(), 0, 1) == '_')
{
Expand Down Expand Up @@ -322,6 +326,19 @@ private static function preControllerHook(&$params, &$URI, &$class, &$method)
}

Route::setCurrentRoute($route);

// If the current route is an anonymous route, we must prevent
// the execution of their 'traditional' counterpart (if exists)
if(is_callable($route->getAction()))
{
$RTR = &load_class('Router', 'core');
$class = Route::DEFAULT_CONTROLLER;
if(!class_exists($class))
{
require_once APPPATH.'/controllers/'. Route::DEFAULT_CONTROLLER .'.php';
}
$method = 'index';
}
}

/**
Expand Down
40 changes: 20 additions & 20 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,15 @@ public function hasParam($name)
*/
public function buildUrl($params)
{
$defaultParams = RouteBuilder::getDefaultParams();
$defaults = RouteBuilder::getDefaultParams();

if(is_string($params))
if(!is_array($params))
{
$params = [ '*' => $params ];
}
else
{
if(!is_array($params))
if(!empty($params) && count($this->params) == 1)
{
$params = [ $this->params[0]->getName() => $params ];
}
else
{
$params = [];
}
Expand All @@ -392,23 +392,23 @@ public function buildUrl($params)
{
$name = $param->getName();

if(!$param->isOptional())
if(!$param->isOptional() && !isset($defaults[$name]) && !isset($params[$param->getName()]))
{
if(!isset($defaultParams[$name]) && !isset($params[$param->getName()]))
{
throw new \Exception('Missing "' . $name .'" parameter for "' . $this->getName() . '" route');
}
throw new \Exception('Missing "' . $name .'" parameter for "' . $this->getName() . '" route');
}

if(isset($defaultParams[$name]))
{
$param->value = $defaultParams[$param->getName()];
}
if(isset($defaults[$name]))
{
$param->value = $defaults[$param->getName()];
}

if(isset($params[$param->getName()]))
{
$param->value = $params[$param->getName()];
}
if(isset($params[$param->getName()]))
{
$param->value = $params[$param->getName()];
}

if(isset($defaults[$name]) || isset($params[$param->getName()]))
{
$path = str_replace($param->getSegment(), $param->value, $path);
}
else
Expand Down

0 comments on commit ba459d1

Please sign in to comment.