Skip to content

Commit 9741243

Browse files
committed
✨ implement a trailing '->name()' call on route http methods
Signed-off-by: otengkwame <[email protected]>
1 parent 7f821c2 commit 9741243

File tree

1 file changed

+121
-45
lines changed

1 file changed

+121
-45
lines changed

Core/core/Route/Route.php

Lines changed: 121 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ class Route
9797
*/
9898
protected static $namedRoutes = [];
9999

100+
/**
101+
* From variable
102+
*
103+
* @var array
104+
*/
105+
protected static $from;
106+
107+
/**
108+
* Options variable
109+
*
110+
* @var array
111+
*/
112+
protected static $options = [];
113+
100114
/**
101115
* Nested Group variable
102116
*
@@ -304,6 +318,24 @@ public function redirect()
304318
return $this;
305319
}
306320

321+
/**
322+
* Set a name for defined route
323+
*
324+
* @param string $name
325+
* @return string|mixed
326+
*/
327+
public static function name($name)
328+
{
329+
330+
if (isset(self::$namedRoutes[$name])) {
331+
return self::$namedRoutes[$name];
332+
} else {
333+
static::$namedRoutes[$name] = static::$from;
334+
}
335+
336+
return null;
337+
}
338+
307339
/**
308340
* Get a route with a given name
309341
*
@@ -314,6 +346,16 @@ public function named($name = '')
314346
return !empty($name) ? static::name($name) : '';
315347
}
316348

349+
/**
350+
* Get the name of the defined route.
351+
*
352+
* @return string The name of the route.
353+
*/
354+
public static function getName($name = '')
355+
{
356+
return (new static)->named($name);
357+
}
358+
317359
/**
318360
* With method
319361
*
@@ -489,8 +531,8 @@ protected static function createRoute($from, $to, $options = [], $nested = false
489531
// Apply our routes
490532
static::$temporaryRoutes[$from] = $to;
491533

492-
$prefix = is_null(static::$prefix) ? '' : static::$prefix . '/';
493-
$group = is_null(static::$group) ? '' : static::$group . '/';
534+
$prefix = is_null(static::$prefix) || empty(static::$prefix) ? '' : static::$prefix . '/';
535+
$group = is_null(static::$group) || empty(static::$group) ? '' : static::$group . '/';
494536

495537
$from = static::$nestedGroup . $prefix . $group . $from;
496538

@@ -509,94 +551,133 @@ protected static function createRoute($from, $to, $options = [], $nested = false
509551

510552
static::$nestedGroup = '';
511553
}
554+
512555
}
513556

514557

515558
public static function any($from, $to, $options = [], $nested = false)
516559
{
517560
static::createRoute($from, $to, $options, $nested);
561+
562+
static::$options = $options;
563+
// Return a new instance of the Route class
564+
return new static;
518565
}
519566

520567
/* --------------------------------------------------------------
521568
* HTTP VERB ROUTING
522569
* ------------------------------------------------------------ */
523570

524571
/**
525-
* Get route
572+
* Route a GET request
526573
*
527-
* @param string $from
528-
* @param string $to
529-
* @return void
574+
* @param string $from The URI pattern to match
575+
* @param string $to The destination of the route
576+
* @param array $options An array of options for the route
577+
* @param callable|bool $nested A callable function to group nested routes, or boolean to check if it's nested
578+
* @return self The Route class instance
530579
*/
531580
public static function get($from, $to, $options = [], $nested = false)
532581
{
582+
// Check if the current request method is GET
533583
if (static::methodIs('GET')) {
584+
// Create the route
534585
static::createRoute($from, $to, $options, $nested);
535586
}
587+
588+
static::$options = $options;
589+
// Return a new instance of the Route class
590+
return new static;
536591
}
537592

538593
/**
539-
* Post route
594+
* Route a POST request
540595
*
541-
* @param string $from
542-
* @param string $to
543-
* @return void
596+
* @param string $from The URI pattern to match
597+
* @param string $to The destination of the route
598+
* @param array $options An array of options for the route
599+
* @param callable|bool $nested A callable function to group nested routes, or boolean to check if it's nested
600+
* @return self The Route class instance
544601
*/
545602
public static function post($from, $to, $options = [], $nested = false)
546603
{
547604
if (static::methodIs('POST')) {
548605
static::createRoute($from, $to, $options, $nested);
549606
}
607+
608+
static::$options = $options;
609+
// Return a new instance of the Route class
610+
return new static;
550611
}
551612

552613
/**
553-
* Put route
614+
* Route a PUT request
554615
*
555-
* @param string $from
556-
* @param string $to
557-
* @return void
616+
* @param string $from The URI pattern to match
617+
* @param string $to The destination of the route
618+
* @param array $options An array of options for the route
619+
* @param callable|bool $nested A callable function to group nested routes, or boolean to check if it's nested
620+
* @return self The Route class instance
558621
*/
559622
public static function put($from, $to, $options = [], $nested = false)
560623
{
561624
if (static::methodIs('PUT')) {
562625
static::createRoute($from, $to, $options, $nested);
563626
}
627+
628+
static::$options = $options;
629+
// Return a new instance of the Route class
630+
return new static;
564631
}
565632

566633
/**
567-
* Delete route
634+
* Route a DELETE request
568635
*
569-
* @param string $from
570-
* @param string $to
571-
* @return void
636+
* @param string $from The URI pattern to match
637+
* @param string $to The destination of the route
638+
* @param array $options An array of options for the route
639+
* @param callable|bool $nested A callable function to group nested routes, or boolean to check if it's nested
640+
* @return self The Route class instance
572641
*/
573642
public static function delete($from, $to, $options = [], $nested = false)
574643
{
575644
if (static::methodIs('DELETE')) {
576645
static::createRoute($from, $to, $options, $nested);
577646
}
647+
648+
static::$options = $options;
649+
// Return a new instance of the Route class
650+
return new static;
578651
}
579652

580653
/**
581-
* Patch route
654+
* Route a PATCH request
582655
*
583-
* @param string $from
584-
* @param string $to
585-
* @return void
656+
* @param string $from The URI pattern to match
657+
* @param string $to The destination of the route
658+
* @param array $options An array of options for the route
659+
* @param callable|bool $nested A callable function to group nested routes, or boolean to check if it's nested
660+
* @return self The Route class instance
586661
*/
587662
public static function patch($from, $to, $options = [], $nested = false)
588663
{
589664
if (static::methodIs('PATCH')) {
590665
static::createRoute($from, $to, $options, $nested);
591666
}
667+
668+
static::$options = $options;
669+
// Return a new instance of the Route class
670+
return new static;
592671
}
593672

594673
/**
595-
* Head route
674+
* Route a HEAD request
596675
*
597-
* @param string $from
598-
* @param string $to
599-
* @return void
676+
* @param string $from The URI pattern to match
677+
* @param string $to The destination of the route
678+
* @param array $options An array of options for the route
679+
* @param callable|bool $nested A callable function to group nested routes, or boolean to check if it's nested
680+
* @return self The Route class instance
600681
*/
601682
public static function head($from, $to, $options = [], $nested = false)
602683
{
@@ -606,14 +687,20 @@ public static function head($from, $to, $options = [], $nested = false)
606687
) {
607688
static::createRoute($from, $to, $options, $nested);
608689
}
690+
691+
static::$options = $options;
692+
// Return a new instance of the Route class
693+
return new static;
609694
}
610695

611696
/**
612-
* Options route
697+
* Route an OPTIONS request
613698
*
614-
* @param string $from
615-
* @param string $to
616-
* @return void
699+
* @param string $from The URI pattern to match
700+
* @param string $to The destination of the route
701+
* @param array $options An array of options for the route
702+
* @param callable|bool $nested A callable function to group nested routes, or boolean to check if it's nested
703+
* @return self The Route class instance
617704
*/
618705
public static function options($from, $to, $options = [], $nested = false)
619706
{
@@ -623,6 +710,10 @@ public static function options($from, $to, $options = [], $nested = false)
623710
) {
624711
static::createRoute($from, $to, $options, $nested);
625712
}
713+
714+
static::$options = $options;
715+
// Return a new instance of the Route class
716+
return new static;
626717
}
627718

628719
/**
@@ -1076,21 +1167,6 @@ public static function module($name, Closure $callable = null)
10761167
static::group($name, $callable);
10771168
}
10781169

1079-
/**
1080-
* Set a name for defined route
1081-
*
1082-
* @param string $name
1083-
* @return string|mixed
1084-
*/
1085-
public static function name($name)
1086-
{
1087-
if (isset(self::$namedRoutes[$name])) {
1088-
return self::$namedRoutes[$name];
1089-
}
1090-
1091-
return null;
1092-
}
1093-
10941170
/**
10951171
* Easily block access to any number of routes by setting
10961172
* that route to an empty path ('').

0 commit comments

Comments
 (0)