mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-20 02:01:26 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f3c6015a59 | |||
| e105f266e3 | |||
| a49d7c13b6 | |||
| 4778a8f29e |
@@ -89,6 +89,7 @@ class InputItem implements ArrayAccess, IInputItem, IteratorAggregate
|
||||
return isset($this->value[$offset]);
|
||||
}
|
||||
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetGet($offset): ?self
|
||||
{
|
||||
if ($this->offsetExists($offset) === true) {
|
||||
|
||||
@@ -7,22 +7,22 @@ use Pecee\Http\Request;
|
||||
class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
{
|
||||
protected array $urls = [
|
||||
'index' => '',
|
||||
'create' => 'create',
|
||||
'store' => '',
|
||||
'show' => '',
|
||||
'edit' => 'edit',
|
||||
'update' => '',
|
||||
'index' => '',
|
||||
'create' => 'create',
|
||||
'store' => '',
|
||||
'show' => '',
|
||||
'edit' => 'edit',
|
||||
'update' => '',
|
||||
'destroy' => '',
|
||||
];
|
||||
|
||||
protected array $methodNames = [
|
||||
'index' => 'index',
|
||||
'create' => 'create',
|
||||
'store' => 'store',
|
||||
'show' => 'show',
|
||||
'edit' => 'edit',
|
||||
'update' => 'update',
|
||||
'index' => 'index',
|
||||
'create' => 'create',
|
||||
'store' => 'store',
|
||||
'show' => 'show',
|
||||
'edit' => 'edit',
|
||||
'update' => 'update',
|
||||
'destroy' => 'destroy',
|
||||
];
|
||||
|
||||
@@ -68,12 +68,15 @@ class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
*/
|
||||
public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
|
||||
{
|
||||
$url = array_search($name, $this->names, true);
|
||||
if ($url !== false) {
|
||||
return rtrim($this->url . $this->urls[$url], '/') . '/';
|
||||
$url = parent::findUrl($method, $parameters, $name);
|
||||
|
||||
$action = array_search($name, $this->names, true);
|
||||
|
||||
if ($action !== false) {
|
||||
return $url . $this->urls[$action];
|
||||
}
|
||||
|
||||
return $this->url;
|
||||
return $url;
|
||||
}
|
||||
|
||||
protected function call($method): bool
|
||||
@@ -172,12 +175,12 @@ class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
$this->name = $name;
|
||||
|
||||
$this->names = [
|
||||
'index' => $this->name . '.index',
|
||||
'create' => $this->name . '.create',
|
||||
'store' => $this->name . '.store',
|
||||
'show' => $this->name . '.show',
|
||||
'edit' => $this->name . '.edit',
|
||||
'update' => $this->name . '.update',
|
||||
'index' => $this->name . '.index',
|
||||
'create' => $this->name . '.create',
|
||||
'store' => $this->name . '.store',
|
||||
'show' => $this->name . '.show',
|
||||
'edit' => $this->name . '.edit',
|
||||
'update' => $this->name . '.update',
|
||||
'destroy' => $this->name . '.destroy',
|
||||
];
|
||||
|
||||
|
||||
@@ -443,12 +443,8 @@ class Router
|
||||
}
|
||||
}
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
if ($e instanceof Exception) {
|
||||
return $this->handleException($e);
|
||||
}
|
||||
|
||||
return $this->handleException(new Exception($e->getMessage(), $e->getCode()));
|
||||
} catch (Exception $e) {
|
||||
return $this->handleException($e);
|
||||
}
|
||||
|
||||
if ($methodNotAllowed === true) {
|
||||
|
||||
@@ -66,4 +66,20 @@ class RouterResourceTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
}
|
||||
|
||||
public function testResourceUrls()
|
||||
{
|
||||
TestRouter::resource('/resource', 'ResourceController')->name('resource');
|
||||
|
||||
TestRouter::debugOutputNoReset('/resource');
|
||||
|
||||
$this->assertEquals('/resource/3/create/', TestRouter::router()->getUrl('resource.create', ['id' => 3]));
|
||||
$this->assertEquals('/resource/3/edit/', TestRouter::router()->getUrl('resource.edit', ['id' => 3]));
|
||||
$this->assertEquals('/resource/3/', TestRouter::router()->getUrl('resource.update', ['id' => 3]));
|
||||
$this->assertEquals('/resource/3/', TestRouter::router()->getUrl('resource.destroy', ['id' => 3]));
|
||||
$this->assertEquals('/resource/3/', TestRouter::router()->getUrl('resource.delete', ['id' => 3]));
|
||||
$this->assertEquals('/resource/', TestRouter::router()->getUrl('resource'));
|
||||
|
||||
TestRouter::router()->reset();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user