mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-15 18:23:26 +03:00
Fixed Resource-type not respecting parameters when using getUrl (issue: #666)
This commit is contained in:
@@ -7,22 +7,22 @@ use Pecee\Http\Request;
|
|||||||
class RouteResource extends LoadableRoute implements IControllerRoute
|
class RouteResource extends LoadableRoute implements IControllerRoute
|
||||||
{
|
{
|
||||||
protected array $urls = [
|
protected array $urls = [
|
||||||
'index' => '',
|
'index' => '',
|
||||||
'create' => 'create',
|
'create' => 'create',
|
||||||
'store' => '',
|
'store' => '',
|
||||||
'show' => '',
|
'show' => '',
|
||||||
'edit' => 'edit',
|
'edit' => 'edit',
|
||||||
'update' => '',
|
'update' => '',
|
||||||
'destroy' => '',
|
'destroy' => '',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected array $methodNames = [
|
protected array $methodNames = [
|
||||||
'index' => 'index',
|
'index' => 'index',
|
||||||
'create' => 'create',
|
'create' => 'create',
|
||||||
'store' => 'store',
|
'store' => 'store',
|
||||||
'show' => 'show',
|
'show' => 'show',
|
||||||
'edit' => 'edit',
|
'edit' => 'edit',
|
||||||
'update' => 'update',
|
'update' => 'update',
|
||||||
'destroy' => 'destroy',
|
'destroy' => 'destroy',
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -68,12 +68,15 @@ class RouteResource extends LoadableRoute implements IControllerRoute
|
|||||||
*/
|
*/
|
||||||
public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
|
public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
|
||||||
{
|
{
|
||||||
$url = array_search($name, $this->names, true);
|
$url = parent::findUrl($method, $parameters, $name);
|
||||||
if ($url !== false) {
|
|
||||||
return rtrim($this->url . $this->urls[$url], '/') . '/';
|
$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
|
protected function call($method): bool
|
||||||
@@ -172,12 +175,12 @@ class RouteResource extends LoadableRoute implements IControllerRoute
|
|||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
|
|
||||||
$this->names = [
|
$this->names = [
|
||||||
'index' => $this->name . '.index',
|
'index' => $this->name . '.index',
|
||||||
'create' => $this->name . '.create',
|
'create' => $this->name . '.create',
|
||||||
'store' => $this->name . '.store',
|
'store' => $this->name . '.store',
|
||||||
'show' => $this->name . '.show',
|
'show' => $this->name . '.show',
|
||||||
'edit' => $this->name . '.edit',
|
'edit' => $this->name . '.edit',
|
||||||
'update' => $this->name . '.update',
|
'update' => $this->name . '.update',
|
||||||
'destroy' => $this->name . '.destroy',
|
'destroy' => $this->name . '.destroy',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -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