Skip to content

Commit

Permalink
Add support for not encoding slashes in wildcard last parameter (#500)
Browse files Browse the repository at this point in the history
* Add failing test

* Add/update failing tests

* Add support for not URI-encoding last route parameter when it has a wildcard regex

* Wip
  • Loading branch information
bakerkretzmar committed Jan 28, 2022
1 parent cbd1843 commit ed5c2e9
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
vendor/
*.cache
*.log
composer.lock
phpunit.xml
2 changes: 1 addition & 1 deletion src/Ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function nameKeyedRoutes()

return $routes->merge($fallbacks)
->map(function ($route) use ($bindings) {
return collect($route)->only(['uri', 'methods'])
return collect($route)->only(['uri', 'methods', 'wheres'])
->put('domain', $route->domain())
->put('bindings', $bindings[$route->getName()] ?? [])
->when($middleware = config('ziggy.middleware'), function ($collection) use ($middleware, $route) {
Expand Down
5 changes: 5 additions & 0 deletions src/js/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class Route {
this.name = name;
this.definition = definition;
this.bindings = definition.bindings ?? {};
this.wheres = definition.wheres ?? {};
this.config = config;
}

Expand Down Expand Up @@ -85,6 +86,10 @@ export default class Route {
throw new Error(`Ziggy error: '${segment}' parameter is required for route '${this.name}'.`)
}

if (this.parameterSegments[this.parameterSegments.length - 1].name === segment && this.wheres[segment] === '.*') {
return params[segment] ?? '';
}

return encodeURIComponent(params[segment] ?? '');
}).replace(/\/+$/, '');
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/CommandRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use Tests\TestCase;

class CommandRouteGeneratorTest extends TestCase
Expand Down Expand Up @@ -44,6 +43,7 @@ public function can_generate_file_with_named_routes()
{
$router = app('router');
$router->get('posts/{post}/comments', $this->noop())->name('postComments.index');
$router->get('slashes/{slug}', $this->noop())->where('slug', '.*')->name('slashes');
$router->getRoutes()->refreshNameLookups();

Artisan::call('ziggy:generate');
Expand Down Expand Up @@ -83,6 +83,7 @@ public function can_generate_file_with_config_applied()
config(['ziggy.except' => ['admin.*']]);
$router = app('router');
$router->get('posts/{post}/comments', $this->noop())->name('postComments.index');
$router->get('slashes/{slug}', $this->noop())->where('slug', '.*')->name('slashes');
$router->get('admin', $this->noop())->name('admin.dashboard'); // Excluded, should NOT be present in file
$router->getRoutes()->refreshNameLookups();

Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/ZiggyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,27 @@ public function can_include_port()
]);
}

/** @test */
public function can_include_wheres()
{
app('router')->post('slashes/{slug}', function () {
return response()->json(new Ziggy);
})->where('slug', '.*')->name('slashes');
app('router')->getRoutes()->refreshNameLookups();

$this->post('http://ziggy.dev/slashes/foo/bar')
->assertJson([
'routes' => [
'slashes' => [
'uri' => 'slashes/{slug}',
'wheres' => [
'slug' => '.*',
],
],
],
]);
}

/** @test */
public function can_include_only_middleware_set_in_config()
{
Expand Down Expand Up @@ -396,6 +417,9 @@ public function can_order_fallback_routes_last()
$expected['fallback'] = [
'uri' => '{fallbackPlaceholder}',
'methods' => ['GET', 'HEAD'],
'wheres' => [
'fallbackPlaceholder' => '.*',
],
];

$this->assertSame($expected, $routes);
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/ziggy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Ziggy = {"url":"http:\/\/ziggy.dev","port":null,"defaults":{},"routes":{"postComments.index":{"uri":"posts\/{post}\/comments","methods":["GET","HEAD"]}}};
const Ziggy = {"url":"http:\/\/ziggy.dev","port":null,"defaults":{},"routes":{"postComments.index":{"uri":"posts\/{post}\/comments","methods":["GET","HEAD"]},"slashes":{"uri":"slashes\/{slug}","methods":["GET","HEAD"],"wheres":{"slug":".*"}}}};

if (typeof window !== 'undefined' && typeof window.Ziggy !== 'undefined') {
Object.assign(Ziggy.routes, window.Ziggy.routes);
Expand Down
19 changes: 15 additions & 4 deletions tests/js/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const defaultZiggy = {
port: null,
defaults: { locale: 'en' },
routes: {
'home': {
home: {
uri: '/',
methods: ['GET', 'HEAD'],
},
Expand Down Expand Up @@ -96,11 +96,11 @@ const defaultZiggy = {
uri: 'subscribers/{subscriber}/conversations/{type}/{conversation_id?}',
methods: ['GET', 'HEAD'],
},
'optional': {
optional: {
uri: 'optional/{id}/{slug?}',
methods: ['GET', 'HEAD'],
},
'optionalId': {
optionalId: {
uri: 'optionalId/{type}/{id?}',
methods: ['GET', 'HEAD'],
},
Expand Down Expand Up @@ -134,10 +134,17 @@ const defaultZiggy = {
uri: 'strict-download/file{extension}',
methods: ['GET', 'HEAD'],
},
'pages': {
pages: {
uri: '{page}',
methods: ['GET', 'HEAD'],
},
slashes: {
uri: 'slashes/{encoded}/{slug}',
methods: ['GET', 'HEAD'],
wheres: {
slug: '.*',
},
},
},
};

Expand Down Expand Up @@ -525,6 +532,10 @@ describe('route()', () => {
same(route('pages.optionalExtension', '.html'), 'https://ziggy.dev/download/file.html');
same(route('pages.optionalExtension', { extension: '.pdf' }), 'https://ziggy.dev/download/file.pdf');
});

test('can skip encoding slashes inside last parameter when explicitly allowed', () => {
same(route('slashes', ['one/two', 'three/four']), 'https://ziggy.dev/slashes/one%2Ftwo/three/four')
});
});

describe('has()', () => {
Expand Down

0 comments on commit ed5c2e9

Please sign in to comment.