Skip to content

Commit c1c59f5

Browse files
committed
🔨 improve Route to comply with routing standards
Signed-off-by: otengkwame <developerkwame@gmail.com>
1 parent 4c32db9 commit c1c59f5

1 file changed

Lines changed: 37 additions & 16 deletions

File tree

Core/core/Route/Route.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,10 @@ public static function webResource($name, $hasController = true)
672672

673673
$name = str_replace('.', '/', implode('.', $name));
674674

675-
static::get($name . '/list', $moc . '/index');
676-
static::get($name . '/show/(:any)', $moc . '/show/$1');
675+
static::get($name . '/index', $moc . '/index');
677676
static::get($name . '/create', $moc . '/create');
678-
static::post($name . '/save', $moc . '/store');
677+
static::post($name . '/store', $moc . '/store');
678+
static::get($name . '/show/(:any)', $moc . '/show/$1');
679679
static::get($name . '/edit/(:any)', $moc . '/edit/$1');
680680
static::put($name . '/update/(:any)', $moc . '/update/$1');
681681
static::delete($name . '/delete/(:any)', $moc . '/delete/$1');
@@ -767,11 +767,9 @@ public static function apiResource($name, $hasController = true)
767767

768768
$name = str_replace('.', '/', implode('.', $name));
769769

770-
static::get($name . '/list', $moc . '/index');
770+
static::get($name . '/index', $moc . '/index');
771+
static::post($name . '/store', $moc . '/store');
771772
static::get($name . '/show/(:any)', $moc . '/show/$1');
772-
static::get($name . '/create', $moc . '/create');
773-
static::post($name . '/save', $moc . '/store');
774-
static::get($name . '/edit/(:any)', $moc . '/edit/$1');
775773
static::put($name . '/update/(:any)', $moc . '/update/$1');
776774
static::delete($name . '/delete/(:any)', $moc . '/delete/$1');
777775
}
@@ -788,6 +786,29 @@ public static function api($name, $hasController = true)
788786
static::apiResource($name, $hasController);
789787
}
790788

789+
/**
790+
* Singleton Resource method
791+
*
792+
* @param string $name i.e. module/controller name
793+
* @param boolean $hasController
794+
* @return void
795+
*/
796+
public static function singleton($name, $hasController = true)
797+
{
798+
$name = str_replace('/', '.', $name);
799+
$name = explode('.', $name);
800+
$module = $name[0];
801+
$controller = !isset($name[1]) ? $module : $name[1];
802+
803+
$moc = static::setMOC($module, $controller, $hasController);
804+
805+
$name = str_replace('.', '/', implode('.', $name));
806+
807+
static::get($name . '/show/(:any)', $moc . '/show/$1');
808+
static::get($name . '/edit/(:any)', $moc . '/edit/$1');
809+
static::put($name . '/update/(:any)', $moc . '/update/$1');
810+
}
811+
791812
/**
792813
* Partial Web Resource which
793814
* Creates partial resource routes
@@ -854,7 +875,7 @@ public static function http($httpMethod, $route, $signature)
854875
* @param string $name The name of the controller to route to.
855876
* @param array $options A list of possible ways to customize the routing.
856877
*/
857-
public static function resources($name, $options = [], $nested = false, $hasController = true)
878+
public static function resource($name, $options = [], $nested = false, $hasController = true)
858879
{
859880
if (empty($name)) {
860881
return;
@@ -909,9 +930,9 @@ public static function resources($name, $options = [], $nested = false, $hasCont
909930

910931
static::get($name, $moc . '/index' . $nestOffset, null, $nested);
911932
static::get($name . '/create', $moc . '/create' . $nestOffset, null, $nested);
912-
static::get($name . '/' . '(:any)' . '/edit', $moc . '/edit' . $nestOffset . '/$' . (1 + $offset), null, $nested);
933+
static::post($name . '/store', $moc . '/store' . $nestOffset, null, $nested);
913934
static::get($name . '/' . '(:any)', $moc . '/show' . $nestOffset . '/$' . (1 + $offset), null, $nested);
914-
static::post($name . '/save', $moc . '/store' . $nestOffset, null, $nested);
935+
static::get($name . '/' . '(:any)' . '/edit', $moc . '/edit' . $nestOffset . '/$' . (1 + $offset), null, $nested);
915936
static::put($name . '/' . '(:any)' . '/update', $moc . '/update' . $nestOffset . '/$' . (1 + $offset), null, $nested);
916937
static::delete($name . '/' . '(:any)' . '/delete', $moc . '/delete' . $nestOffset . '/$' . (1 + $offset), null, $nested);
917938
}
@@ -952,19 +973,19 @@ private static function setMOC($module, $controller, $hasController)
952973
private static function setRouteSignature($name, $method, $moc)
953974
{
954975
if (in_array('index', $method)) {
955-
static::get($name . '/list', $moc . '/index');
956-
}
957-
958-
if (in_array('show', $method)) {
959-
static::get($name . '/show/(:any)', $moc . '/show/$1');
976+
static::get($name . '/index', $moc . '/index');
960977
}
961978

962979
if (in_array('create', $method)) {
963980
static::get($name . '/create', $moc . '/create');
964981
}
965982

966983
if (in_array('store', $method)) {
967-
static::post($name . '/save', $moc . '/store');
984+
static::post($name . '/store', $moc . '/store');
985+
}
986+
987+
if (in_array('show', $method)) {
988+
static::get($name . '/show/(:any)', $moc . '/show/$1');
968989
}
969990

970991
if (in_array('edit', $method)) {

0 commit comments

Comments
 (0)