Skip to content

Commit

Permalink
Merge pull request #302 from tightenco/jbk/subfolder-issue-295
Browse files Browse the repository at this point in the history
Allow routing to apps installed in subfolders
  • Loading branch information
bakerkretzmar authored Jun 26, 2020
2 parents 49cd9d8 + 0a7a64b commit a995306
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
3 changes: 2 additions & 1 deletion dist/js/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,8 @@ var route_Router = /*#__PURE__*/function (_String) {
key: "params",
get: function get() {
var namedRoute = this.ziggy.namedRoutes[this.current()];
return _extends(this.extractParams(window.location.hostname, namedRoute.domain || '', '.'), this.extractParams(window.location.pathname.slice(1), namedRoute.uri, '/'));
var pathname = window.location.pathname.replace(this.ziggy.baseUrl.split('://')[1].split('/')[1], '').replace(/^\/+/, '');
return _extends(this.extractParams(window.location.hostname, namedRoute.domain || '', '.'), this.extractParams(pathname, namedRoute.uri, '/'));
}
}]);

Expand Down
2 changes: 1 addition & 1 deletion dist/js/route.min.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"name": "ziggy-js",
"version": "0.9.1",
"description": "Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.",
"keywords": ["laravel", "routes", "ziggy"],
"keywords": [
"laravel",
"routes",
"ziggy"
],
"module": "src/js/route.js",
"main": "dist/js/route.js",
"browser": "dist/js/route.min.js",
Expand Down
6 changes: 5 additions & 1 deletion src/js/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,18 @@ class Router extends String {
get params() {
const namedRoute = this.ziggy.namedRoutes[this.current()];

let pathname = window.location.pathname
.replace(this.ziggy.baseUrl.split('://')[1].split('/')[1], '')
.replace(/^\/+/, '');

return Object.assign(
this.extractParams(
window.location.hostname,
namedRoute.domain || '',
'.'
),
this.extractParams(
window.location.pathname.slice(1),
pathname,
namedRoute.uri,
'/'
)
Expand Down
33 changes: 32 additions & 1 deletion tests/js/test.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ global.Ziggy = {
uri: 'optionalId/{type}/{id?}',
methods: ['GET', 'HEAD'],
domain: null
}
},
'products.show': {
uri: '{country?}/{language?}/products/{id}',
methods: ['GET', 'HEAD'],
domain: null
},
},
baseUrl: 'http://myapp.dev/',
baseProtocol: 'http',
Expand All @@ -107,6 +112,29 @@ describe('route()', function() {
assert.equal('http://myapp.dev/posts', route('posts.index'));
});

it('Can handle routing for apps in a subfolder', function() {
let orgBaseUrl = Ziggy.baseUrl;
let orgBaseDomain = Ziggy.baseDomain;
let orgWindow = global.window;

global.Ziggy.baseUrl = 'http://domain.com/subfolder/';
global.Ziggy.baseDomain = 'domain.com';

global.window = {
location: {
href: 'http://domain.com/subfolder/ph/en/products/4',
hostname: 'domain.com',
pathname: '/subfolder/ph/en/products/4',
},
};

assert.deepStrictEqual(route().params, { country: 'ph', language: 'en', id: '4' });

global.Ziggy.baseUrl = orgBaseUrl;
global.Ziggy.baseDomain = orgBaseDomain;
global.window = orgWindow;
});

it('Should return URL when run without params on a route without params , with default params', function() {
assert.equal(
'http://myapp.dev/en/posts',
Expand Down Expand Up @@ -882,16 +910,19 @@ describe('route()', function() {
});

it('Should combine dynamic params from the domain and the URI', function() {
global.window.location.href = 'http://tighten.myapp.dev/users/1';
global.window.location.hostname = 'tighten.myapp.dev';
global.window.location.pathname = '/users/1';

assert.deepStrictEqual(route().params, { team: 'tighten', id: '1' });

global.window.location.href = 'http://' + global.Ziggy.baseDomain + '/posts/1';
global.window.location.hostname = global.Ziggy.baseDomain;
global.window.location.pathname = '/posts/1';

assert.deepStrictEqual(route().params, { post: '1' });

global.window.location.href = 'http://myapp.dev/events/1/venues/2';
global.window.location.pathname = '/events/1/venues/2';

assert.deepStrictEqual(route().params, { event: '1', venue: '2' });
Expand Down

0 comments on commit a995306

Please sign in to comment.