Skip to content

Commit e792c55

Browse files
committed
🔨 improve on prefix() and group() methods for nested route paths
Signed-off-by: otengkwame <[email protected]>
1 parent 05b6573 commit e792c55

File tree

1 file changed

+23
-76
lines changed

1 file changed

+23
-76
lines changed

Core/core/Route/Route.php

Lines changed: 23 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,17 @@ class Route
7979
/**
8080
* Route Prefix variable
8181
*
82-
* @var string
82+
* @var mixed
8383
*/
8484
protected static $prefix = null;
8585

86+
/**
87+
* Route Group variable
88+
*
89+
* @var mixed
90+
*/
91+
protected static $group = null;
92+
8693
/**
8794
* Named routes
8895
*
@@ -317,7 +324,7 @@ public function named($name = '')
317324
public function with($key, $value = null)
318325
{
319326

320-
ci('load')->library('session');
327+
ci()->use->library('session');
321328

322329
if (is_array($key)) {
323330
ci()->session->set_flashdata($key);
@@ -364,7 +371,7 @@ public function withError($message)
364371
*/
365372
public function withInput($post = [])
366373
{
367-
ci('load')->library('session');
374+
ci()->use->library('session');
368375

369376
if (empty($post)) {
370377
$post = ci()->input->post();
@@ -483,8 +490,9 @@ protected static function createRoute($from, $to, $options = [], $nested = false
483490
static::$temporaryRoutes[$from] = $to;
484491

485492
$prefix = is_null(static::$prefix) ? '' : static::$prefix . '/';
493+
$group = is_null(static::$group) ? '' : static::$group . '/';
486494

487-
$from = static::$nestedGroup . $prefix . $from;
495+
$from = static::$nestedGroup . $prefix . $group . $from;
488496

489497
// Are we saving the name for this one?
490498
if (isset($options['as']) && !empty($options['as'])) {
@@ -503,71 +511,6 @@ protected static function createRoute($from, $to, $options = [], $nested = false
503511
}
504512
}
505513

506-
/**
507-
* Static Route method
508-
*
509-
* @param string $from
510-
* @param string $to
511-
* @param boolean $nested
512-
* @return void
513-
*/
514-
public static function route($from, $to, $nested = false)
515-
{
516-
$parameterfy = false;
517-
518-
// Allow for array based routes and other symbol routes
519-
if (!is_array($to) && strstr($to, '.')) {
520-
$to = str_replace('.', '/', $to);
521-
}
522-
523-
if (is_array($to)) {
524-
$to = $to[0] . '/' . strtolower($to[1]);
525-
$parameterfy = true;
526-
} elseif (
527-
preg_match('/^([a-zA-Z\_\-0-9\/]+)->([a-zA-Z\_\-0-9\/]+)$/m', $to, $matches)
528-
) {
529-
$to = $matches[1] . '/' . $matches[2];
530-
$parameterfy = true;
531-
} elseif (
532-
preg_match('/^([a-zA-Z\_\-0-9\/]+)::([a-zA-Z\_\-0-9\/]+)$/m', $to, $matches)
533-
) {
534-
$to = $matches[1] . '/' . $matches[2];
535-
$parameterfy = true;
536-
} elseif (
537-
preg_match('/^([a-zA-Z\_\-0-9\/]+)@([a-zA-Z\_\-0-9\/]+)$/m', $to, $matches)
538-
) {
539-
$to = $matches[1] . '/' . $matches[2];
540-
$parameterfy = true;
541-
}
542-
543-
// Do we have a namespace?
544-
if (static::$namespace) {
545-
$from = static::$namespace . '/' . $from;
546-
}
547-
548-
// Account for parameters in the URL if we need to
549-
if ($parameterfy) {
550-
$to = static::parameterfy($from, $to);
551-
}
552-
553-
// Apply our routes
554-
static::$temporaryRoutes[$from] = $to;
555-
556-
$prefix = is_null(static::$prefix) ? '' : static::$prefix . '/';
557-
558-
$from = static::$nestedGroup . $prefix . $from;
559-
560-
static::$routes[$from] = $to;
561-
562-
// Do we have a nested function?
563-
if ($nested && is_callable($nested) && static::$nestedDepth === 0) {
564-
static::$nestedGroup .= rtrim($from, '/') . '/';
565-
static::$nestedDepth += 1;
566-
call_user_func($nested);
567-
568-
static::$nestedGroup = '';
569-
}
570-
}
571514

572515
public static function any($from, $to, $options = [], $nested = false)
573516
{
@@ -1065,11 +1008,11 @@ private static function setRouteHttpMethod($name, $httpMethod, $signature)
10651008
* @param string $name The prefix to add to the routes.
10661009
* @param Closure $callback
10671010
*/
1068-
protected static function prefix($name, Closure $callback)
1011+
public static function prefix($name, Closure $callback)
10691012
{
1070-
static::$prefix = $name;
1013+
static::$group = $name . '/';
10711014
call_user_func($callback);
1072-
static::$prefix = null;
1015+
static::$group = null;
10731016
}
10741017

10751018
/**
@@ -1081,7 +1024,9 @@ protected static function prefix($name, Closure $callback)
10811024
*/
10821025
public static function group($name, Closure $callable = null)
10831026
{
1084-
static::prefix($name, $callable);
1027+
static::$group = $name;
1028+
call_user_func($callable);
1029+
static::$group = null;
10851030
}
10861031

10871032
/**
@@ -1093,7 +1038,7 @@ public static function group($name, Closure $callable = null)
10931038
*/
10941039
public static function module($name, Closure $callable = null)
10951040
{
1096-
static::prefix($name, $callable);
1041+
static::group($name, $callable);
10971042
}
10981043

10991044
/**
@@ -1152,8 +1097,10 @@ public static function clear()
11521097
public static function reset()
11531098
{
11541099
static::$routes = [];
1155-
static::$namedRoutes = [];
1156-
static::$nestedDepth = 0;
1100+
static::$namedRoutes = [];
1101+
static::$nestedDepth = 0;
1102+
static::$group = null;
1103+
static::$prefix = null;
11571104
}
11581105

11591106
/**

0 commit comments

Comments
 (0)