Skip to content

Commit

Permalink
🔨 improve Route to comply with routing standards
Browse files Browse the repository at this point in the history
Signed-off-by: otengkwame <[email protected]>
  • Loading branch information
otengkwame committed May 2, 2023
1 parent 4c32db9 commit c1c59f5
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions Core/core/Route/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,10 +672,10 @@ public static function webResource($name, $hasController = true)

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

static::get($name . '/list', $moc . '/index');
static::get($name . '/show/(:any)', $moc . '/show/$1');
static::get($name . '/index', $moc . '/index');
static::get($name . '/create', $moc . '/create');
static::post($name . '/save', $moc . '/store');
static::post($name . '/store', $moc . '/store');
static::get($name . '/show/(:any)', $moc . '/show/$1');
static::get($name . '/edit/(:any)', $moc . '/edit/$1');
static::put($name . '/update/(:any)', $moc . '/update/$1');
static::delete($name . '/delete/(:any)', $moc . '/delete/$1');
Expand Down Expand Up @@ -767,11 +767,9 @@ public static function apiResource($name, $hasController = true)

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

static::get($name . '/list', $moc . '/index');
static::get($name . '/index', $moc . '/index');
static::post($name . '/store', $moc . '/store');
static::get($name . '/show/(:any)', $moc . '/show/$1');
static::get($name . '/create', $moc . '/create');
static::post($name . '/save', $moc . '/store');
static::get($name . '/edit/(:any)', $moc . '/edit/$1');
static::put($name . '/update/(:any)', $moc . '/update/$1');
static::delete($name . '/delete/(:any)', $moc . '/delete/$1');
}
Expand All @@ -788,6 +786,29 @@ public static function api($name, $hasController = true)
static::apiResource($name, $hasController);
}

/**
* Singleton Resource method
*
* @param string $name i.e. module/controller name
* @param boolean $hasController
* @return void
*/
public static function singleton($name, $hasController = true)
{
$name = str_replace('/', '.', $name);
$name = explode('.', $name);
$module = $name[0];
$controller = !isset($name[1]) ? $module : $name[1];

$moc = static::setMOC($module, $controller, $hasController);

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

static::get($name . '/show/(:any)', $moc . '/show/$1');
static::get($name . '/edit/(:any)', $moc . '/edit/$1');
static::put($name . '/update/(:any)', $moc . '/update/$1');
}

/**
* Partial Web Resource which
* Creates partial resource routes
Expand Down Expand Up @@ -854,7 +875,7 @@ public static function http($httpMethod, $route, $signature)
* @param string $name The name of the controller to route to.
* @param array $options A list of possible ways to customize the routing.
*/
public static function resources($name, $options = [], $nested = false, $hasController = true)
public static function resource($name, $options = [], $nested = false, $hasController = true)
{
if (empty($name)) {
return;
Expand Down Expand Up @@ -909,9 +930,9 @@ public static function resources($name, $options = [], $nested = false, $hasCont

static::get($name, $moc . '/index' . $nestOffset, null, $nested);
static::get($name . '/create', $moc . '/create' . $nestOffset, null, $nested);
static::get($name . '/' . '(:any)' . '/edit', $moc . '/edit' . $nestOffset . '/$' . (1 + $offset), null, $nested);
static::post($name . '/store', $moc . '/store' . $nestOffset, null, $nested);
static::get($name . '/' . '(:any)', $moc . '/show' . $nestOffset . '/$' . (1 + $offset), null, $nested);
static::post($name . '/save', $moc . '/store' . $nestOffset, null, $nested);
static::get($name . '/' . '(:any)' . '/edit', $moc . '/edit' . $nestOffset . '/$' . (1 + $offset), null, $nested);
static::put($name . '/' . '(:any)' . '/update', $moc . '/update' . $nestOffset . '/$' . (1 + $offset), null, $nested);
static::delete($name . '/' . '(:any)' . '/delete', $moc . '/delete' . $nestOffset . '/$' . (1 + $offset), null, $nested);
}
Expand Down Expand Up @@ -952,19 +973,19 @@ private static function setMOC($module, $controller, $hasController)
private static function setRouteSignature($name, $method, $moc)
{
if (in_array('index', $method)) {
static::get($name . '/list', $moc . '/index');
}

if (in_array('show', $method)) {
static::get($name . '/show/(:any)', $moc . '/show/$1');
static::get($name . '/index', $moc . '/index');
}

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

if (in_array('store', $method)) {
static::post($name . '/save', $moc . '/store');
static::post($name . '/store', $moc . '/store');
}

if (in_array('show', $method)) {
static::get($name . '/show/(:any)', $moc . '/show/$1');
}

if (in_array('edit', $method)) {
Expand Down

0 comments on commit c1c59f5

Please sign in to comment.