From d23155421cd35c72a662994a8a0d3b502179b033 Mon Sep 17 00:00:00 2001 From: Mart de Graaf Date: Thu, 9 Feb 2017 10:45:15 +0100 Subject: [PATCH 1/6] Added an configurable option for the secureUrl Option determins if it is http or https. (adds scheme to absolute URL with other host). I had a problem when displaying an URL to the end user, i had to add the scheme myself. --- config/laroute.php | 6 ++++++ src/Console/Commands/LarouteGeneratorCommand.php | 5 +++-- src/templates/laroute.js | 7 ++++++- src/templates/laroute.min.js | 2 +- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/config/laroute.php b/config/laroute.php index 6866c49..0adeea4 100644 --- a/config/laroute.php +++ b/config/laroute.php @@ -25,6 +25,12 @@ */ 'absolute' => false, + /* + * Generate secure absolute URLs (requires absolute=true) + * This will determine which scheme to use on your routes (http or https) + */ + 'secure_url' => true, + /* * The Filter Method * diff --git a/src/Console/Commands/LarouteGeneratorCommand.php b/src/Console/Commands/LarouteGeneratorCommand.php index 5e3d939..7b5c3ba 100644 --- a/src/Console/Commands/LarouteGeneratorCommand.php +++ b/src/Console/Commands/LarouteGeneratorCommand.php @@ -78,7 +78,7 @@ public function fire() ); $this->info("Created: {$filePath}"); - } catch (\Exception $e) { + } catch (\Exception $e){ $this->error($e->getMessage()); } } @@ -103,10 +103,11 @@ protected function getTemplateData() $namespace = $this->getOptionOrConfig('namespace'); $routes = $this->routes->toJSON(); $absolute = $this->config->get('laroute.absolute', false); + $secureUrl = $this->config->get('laroute.secure_url', true); $rootUrl = $this->config->get('app.url', ''); $prefix = $this->config->get('laroute.prefix', ''); - return compact('namespace', 'routes', 'absolute', 'rootUrl', 'prefix'); + return compact('namespace', 'routes', 'absolute', 'secureUrl', 'rootUrl', 'prefix'); } diff --git a/src/templates/laroute.js b/src/templates/laroute.js index f868098..778cf13 100644 --- a/src/templates/laroute.js +++ b/src/templates/laroute.js @@ -5,6 +5,7 @@ var routes = { absolute: $ABSOLUTE$, + secureUrl: $SECUREURL$, rootUrl: '$ROOTURL$', routes : $ROUTES$, prefix: '$PREFIX$', @@ -32,7 +33,7 @@ var qs = this.getRouteQueryString(parameters); if (this.absolute && this.isOtherHost(route)){ - return "//" + route.host + "/" + uri + qs; + return this.getScheme() + route.host + "/" + uri + qs; } return this.getCorrectUrl(uri + qs); @@ -42,6 +43,10 @@ return route.host && route.host != window.location.hostname; }, + getScheme: function (){ + return this.secureUrl ? "https://" : "http://"; + }, + replaceNamedParameters : function (uri, parameters) { uri = uri.replace(/\{(.*?)\??\}/g, function(match, key) { if (parameters.hasOwnProperty(key)) { diff --git a/src/templates/laroute.min.js b/src/templates/laroute.min.js index 2593912..448f1a3 100644 --- a/src/templates/laroute.min.js +++ b/src/templates/laroute.min.js @@ -1 +1 @@ -(function(){var a=function(){var a={absolute:$ABSOLUTE$,rootUrl:"$ROOTURL$",routes:$ROUTES$,prefix:"$PREFIX$",route:function(a,b,c){if(c=c||this.getByName(a))return this.toRoute(c,b)},url:function(a,b){b=b||[];var c=a+"/"+b.join("/");return this.getCorrectUrl(c)},toRoute:function(a,b){var c=this.replaceNamedParameters(a.uri,b),d=this.getRouteQueryString(b);return this.absolute&&this.isOtherHost(a)?"//"+a.host+"/"+c+d:this.getCorrectUrl(c+d)},isOtherHost:function(a){return a.host&&a.host!=window.location.hostname},replaceNamedParameters:function(a,b){return a=a.replace(/\{(.*?)\??\}/g,function(a,c){if(b.hasOwnProperty(c)){var d=b[c];return delete b[c],d}return a}),a=a.replace(/\/\{.*?\?\}/g,"")},getRouteQueryString:function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c+"="+a[c]);return b.length<1?"":"?"+b.join("&")},getByName:function(a){for(var b in this.routes)if(this.routes.hasOwnProperty(b)&&this.routes[b].name===a)return this.routes[b]},getByAction:function(a){for(var b in this.routes)if(this.routes.hasOwnProperty(b)&&this.routes[b].action===a)return this.routes[b]},getCorrectUrl:function(a){var b=this.prefix+"/"+a.replace(/^\/?/,"");return this.absolute?this.rootUrl.replace("//?$/","")+b:b}},b=function(a){if(!a)return"";var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c+'="'+a[c]+'"');return b.join(" ")},c=function(a,c,d){return c=c||a,d=b(d),'"+c+""};return{action:function(b,c){return c=c||{},a.route(b,c,a.getByAction(b))},route:function(b,c){return c=c||{},a.route(b,c)},url:function(b,c){return c=c||{},a.url(b,c)},link_to:function(a,b,d){return a=this.url(a),c(a,b,d)},link_to_route:function(a,b,d,e){var f=this.route(a,d);return c(f,b,e)},link_to_action:function(a,b,d,e){var f=this.action(a,d);return c(f,b,e)}}}.call(this);"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof module&&module.exports?module.exports=a:window.$NAMESPACE$=a}).call(this); \ No newline at end of file +(function(){var a=function(){var a={absolute:$ABSOLUTE$,secureUrl:$SECUREURL$,rootUrl:"$ROOTURL$",routes:$ROUTES$,prefix:"$PREFIX$",route:function(a,b,c){if(c=c||this.getByName(a))return this.toRoute(c,b)},url:function(a,b){b=b||[];var c=a+"/"+b.join("/");return this.getCorrectUrl(c)},toRoute:function(a,b){var c=this.replaceNamedParameters(a.uri,b),d=this.getRouteQueryString(b);return this.absolute&&this.isOtherHost(a)?this.getScheme()+a.host+"/"+c+d:this.getCorrectUrl(c+d)},isOtherHost:function(a){return a.host&&a.host!=window.location.hostname},getScheme:function(){return this.secureUrl?"https://":"http://"},replaceNamedParameters:function(a,b){return a=a.replace(/\{(.*?)\??\}/g,function(a,c){if(b.hasOwnProperty(c)){var d=b[c];return delete b[c],d}return a}),a=a.replace(/\/\{.*?\?\}/g,"")},getRouteQueryString:function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c+"="+a[c]);return b.length<1?"":"?"+b.join("&")},getByName:function(a){for(var b in this.routes)if(this.routes.hasOwnProperty(b)&&this.routes[b].name===a)return this.routes[b]},getByAction:function(a){for(var b in this.routes)if(this.routes.hasOwnProperty(b)&&this.routes[b].action===a)return this.routes[b]},getCorrectUrl:function(a){var b=this.prefix+"/"+a.replace(/^\/?/,"");return this.absolute?this.rootUrl.replace("//?$/","")+b:b}},b=function(a){if(!a)return"";var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c+'="'+a[c]+'"');return b.join(" ")},c=function(a,c,d){return c=c||a,d=b(d),'"+c+""};return{action:function(b,c){return c=c||{},a.route(b,c,a.getByAction(b))},route:function(b,c){return c=c||{},a.route(b,c)},url:function(b,c){return c=c||{},a.url(b,c)},link_to:function(a,b,d){return a=this.url(a),c(a,b,d)},link_to_route:function(a,b,d,e){var f=this.route(a,d);return c(f,b,e)},link_to_action:function(a,b,d,e){var f=this.action(a,d);return c(f,b,e)}}}.call(this);"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof module&&module.exports?module.exports=a:window.$NAMESPACE$=a}).call(this); \ No newline at end of file From 4be40a036613fae64ba68672cfe5061dc16c9fed Mon Sep 17 00:00:00 2001 From: Mart de Graaf Date: Mon, 13 Feb 2017 11:17:30 +0100 Subject: [PATCH 2/6] absoluteRoute as method added This allows you to generate an URL of the current page without having all the other links absolute.. --- src/templates/laroute.js | 33 +++++++++++++++++++++------------ src/templates/laroute.min.js | 2 +- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/templates/laroute.js b/src/templates/laroute.js index 778cf13..545053f 100644 --- a/src/templates/laroute.js +++ b/src/templates/laroute.js @@ -10,14 +10,14 @@ routes : $ROUTES$, prefix: '$PREFIX$', - route : function (name, parameters, route) { + route : function (name, parameters, route, forceAbsolute) { route = route || this.getByName(name); if ( ! route ) { return undefined; } - return this.toRoute(route, parameters); + return this.toRoute(route, parameters, forceAbsolute); }, url: function (url, parameters) { @@ -28,15 +28,16 @@ return this.getCorrectUrl(uri); }, - toRoute : function (route, parameters) { + toRoute : function (route, parameters, forceAbsolute) { var uri = this.replaceNamedParameters(route.uri, parameters); var qs = this.getRouteQueryString(parameters); - if (this.absolute && this.isOtherHost(route)){ - return this.getScheme() + route.host + "/" + uri + qs; + var path = this.getCorrectUrl(uri + qs); + if(this.absolute || forceAbsolute){ + return this.getCorrectAbsoluteUrl(path, route, forceAbsolute); } - return this.getCorrectUrl(uri + qs); + return path; }, isOtherHost: function (route){ @@ -95,14 +96,16 @@ } }, - getCorrectUrl: function (uri) { - var url = this.prefix + '/' + uri.replace(/^\/?/, ''); - - if ( ! this.absolute) { - return url; + getCorrectAbsoluteUrl: function (path, route, forceAbsolute) { + if (this.isOtherHost(route) || (route.host && forceAbsolute)){ + return this.getScheme() + route.host + "/" + path; } + return this.rootUrl.replace('/\/?$/', '') + path; + }, - return this.rootUrl.replace('/\/?$/', '') + url; + getCorrectUrl: function (uri) { + var url = this.prefix + '/' + uri.replace(/^\/?/, ''); + return url; } }; @@ -145,6 +148,12 @@ return routes.route(route, parameters); }, + absoluteRoute : function (route, parameters) { + parameters = parameters || {}; + + return routes.route(route, parameters, true); + }, + // Generate a fully qualified URL to the given path. // $NAMESPACE$.route('url', [params = {}]) url : function (route, parameters) { diff --git a/src/templates/laroute.min.js b/src/templates/laroute.min.js index 448f1a3..743b969 100644 --- a/src/templates/laroute.min.js +++ b/src/templates/laroute.min.js @@ -1 +1 @@ -(function(){var a=function(){var a={absolute:$ABSOLUTE$,secureUrl:$SECUREURL$,rootUrl:"$ROOTURL$",routes:$ROUTES$,prefix:"$PREFIX$",route:function(a,b,c){if(c=c||this.getByName(a))return this.toRoute(c,b)},url:function(a,b){b=b||[];var c=a+"/"+b.join("/");return this.getCorrectUrl(c)},toRoute:function(a,b){var c=this.replaceNamedParameters(a.uri,b),d=this.getRouteQueryString(b);return this.absolute&&this.isOtherHost(a)?this.getScheme()+a.host+"/"+c+d:this.getCorrectUrl(c+d)},isOtherHost:function(a){return a.host&&a.host!=window.location.hostname},getScheme:function(){return this.secureUrl?"https://":"http://"},replaceNamedParameters:function(a,b){return a=a.replace(/\{(.*?)\??\}/g,function(a,c){if(b.hasOwnProperty(c)){var d=b[c];return delete b[c],d}return a}),a=a.replace(/\/\{.*?\?\}/g,"")},getRouteQueryString:function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c+"="+a[c]);return b.length<1?"":"?"+b.join("&")},getByName:function(a){for(var b in this.routes)if(this.routes.hasOwnProperty(b)&&this.routes[b].name===a)return this.routes[b]},getByAction:function(a){for(var b in this.routes)if(this.routes.hasOwnProperty(b)&&this.routes[b].action===a)return this.routes[b]},getCorrectUrl:function(a){var b=this.prefix+"/"+a.replace(/^\/?/,"");return this.absolute?this.rootUrl.replace("//?$/","")+b:b}},b=function(a){if(!a)return"";var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c+'="'+a[c]+'"');return b.join(" ")},c=function(a,c,d){return c=c||a,d=b(d),'"+c+""};return{action:function(b,c){return c=c||{},a.route(b,c,a.getByAction(b))},route:function(b,c){return c=c||{},a.route(b,c)},url:function(b,c){return c=c||{},a.url(b,c)},link_to:function(a,b,d){return a=this.url(a),c(a,b,d)},link_to_route:function(a,b,d,e){var f=this.route(a,d);return c(f,b,e)},link_to_action:function(a,b,d,e){var f=this.action(a,d);return c(f,b,e)}}}.call(this);"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof module&&module.exports?module.exports=a:window.$NAMESPACE$=a}).call(this); \ No newline at end of file +(function(){var a=function(){var a={absolute:$ABSOLUTE$,secureUrl:$SECUREURL$,rootUrl:"$ROOTURL$",routes:$ROUTES$,prefix:"$PREFIX$",route:function(a,b,c,d){if(c=c||this.getByName(a))return this.toRoute(c,b,d)},url:function(a,b){b=b||[];var c=a+"/"+b.join("/");return this.getCorrectUrl(c)},toRoute:function(a,b,c){var d=this.replaceNamedParameters(a.uri,b),e=this.getRouteQueryString(b),f=this.getCorrectUrl(d+e);return this.absolute||c?this.getCorrectAbsoluteUrl(f,a,c):f},isOtherHost:function(a){return a.host&&a.host!=window.location.hostname},getScheme:function(){return this.secureUrl?"https://":"http://"},replaceNamedParameters:function(a,b){return a=a.replace(/\{(.*?)\??\}/g,function(a,c){if(b.hasOwnProperty(c)){var d=b[c];return delete b[c],d}return a}),a=a.replace(/\/\{.*?\?\}/g,"")},getRouteQueryString:function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c+"="+a[c]);return b.length<1?"":"?"+b.join("&")},getByName:function(a){for(var b in this.routes)if(this.routes.hasOwnProperty(b)&&this.routes[b].name===a)return this.routes[b]},getByAction:function(a){for(var b in this.routes)if(this.routes.hasOwnProperty(b)&&this.routes[b].action===a)return this.routes[b]},getCorrectAbsoluteUrl:function(a,b,c){return this.isOtherHost(b)||b.host&&c?this.getScheme()+b.host+"/"+a:this.rootUrl.replace("//?$/","")+a},getCorrectUrl:function(a){var b=this.prefix+"/"+a.replace(/^\/?/,"");return b}},b=function(a){if(!a)return"";var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c+'="'+a[c]+'"');return b.join(" ")},c=function(a,c,d){return c=c||a,d=b(d),'"+c+""};return{action:function(b,c){return c=c||{},a.route(b,c,a.getByAction(b))},route:function(b,c){return c=c||{},a.route(b,c)},absoluteRoute:function(b,c){return c=c||{},a.route(b,c,!0)},url:function(b,c){return c=c||{},a.url(b,c)},link_to:function(a,b,d){return a=this.url(a),c(a,b,d)},link_to_route:function(a,b,d,e){var f=this.route(a,d);return c(f,b,e)},link_to_action:function(a,b,d,e){var f=this.action(a,d);return c(f,b,e)}}}.call(this);"function"==typeof define&&define.amd?define(function(){return a}):"object"==typeof module&&module.exports?module.exports=a:window.$NAMESPACE$=a}).call(this); \ No newline at end of file From ef8bf70b2794ac808dfe65d3e23ccad8c98be3a3 Mon Sep 17 00:00:00 2001 From: Mart de Graaf Date: Mon, 13 Feb 2017 11:29:10 +0100 Subject: [PATCH 3/6] Docs + fix --- src/templates/laroute.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/templates/laroute.js b/src/templates/laroute.js index 545053f..f215aab 100644 --- a/src/templates/laroute.js +++ b/src/templates/laroute.js @@ -140,18 +140,19 @@ return routes.route(name, parameters, routes.getByAction(name)); }, - // Generate a url for a given named route. + // Generate an url for a given named route. // $NAMESPACE$.route('routeName', [params = {}]) route : function (route, parameters) { parameters = parameters || {}; return routes.route(route, parameters); }, - + // Generate an absolute url for a given named route. + // $NAMESPACE$.absoluteRoute('routeName', [params = {}]) absoluteRoute : function (route, parameters) { parameters = parameters || {}; - return routes.route(route, parameters, true); + return routes.route(route, parameters, false, true); }, // Generate a fully qualified URL to the given path. From 195ec4da96ad2e0f50b4cf2da817f353a58ab1f4 Mon Sep 17 00:00:00 2001 From: Mart de Graaf Date: Mon, 13 Feb 2017 13:26:05 +0100 Subject: [PATCH 4/6] Also get correct absolute Url if is another host --- src/templates/laroute.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/templates/laroute.js b/src/templates/laroute.js index f215aab..c2854cf 100644 --- a/src/templates/laroute.js +++ b/src/templates/laroute.js @@ -33,7 +33,7 @@ var qs = this.getRouteQueryString(parameters); var path = this.getCorrectUrl(uri + qs); - if(this.absolute || forceAbsolute){ + if(this.absolute || forceAbsolute || this.isOtherHost(route)){ return this.getCorrectAbsoluteUrl(path, route, forceAbsolute); } From 3d2774f39281c6fdc6683225988f95d899b7371d Mon Sep 17 00:00:00 2001 From: Mart de Graaf Date: Mon, 13 Feb 2017 13:30:31 +0100 Subject: [PATCH 5/6] Fix double slash after the domain --- src/templates/laroute.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/templates/laroute.js b/src/templates/laroute.js index c2854cf..8d09007 100644 --- a/src/templates/laroute.js +++ b/src/templates/laroute.js @@ -98,7 +98,7 @@ getCorrectAbsoluteUrl: function (path, route, forceAbsolute) { if (this.isOtherHost(route) || (route.host && forceAbsolute)){ - return this.getScheme() + route.host + "/" + path; + return this.getScheme() + route.host + path; } return this.rootUrl.replace('/\/?$/', '') + path; }, From 57b769a4dbb9f3ee8ca0e21e4c85c5acaabfb3a7 Mon Sep 17 00:00:00 2001 From: Muah Date: Mon, 13 Mar 2017 13:07:00 +0200 Subject: [PATCH 6/6] add option to exclude route information from output --- config/laroute.php | 8 ++++++-- src/Routes/Collection.php | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/config/laroute.php b/config/laroute.php index 6866c49..bfd2fe7 100644 --- a/config/laroute.php +++ b/config/laroute.php @@ -1,7 +1,6 @@ 'vendor/lord/laroute/src/templates/laroute.js', - + /* * Appends a prefix to URLs. By default the prefix is an empty string. * */ 'prefix' => '', + /* + * what to exclude from the output file + * ex. ['action', 'methods', 'host'] + */ + 'exclude' => [], ]; diff --git a/src/Routes/Collection.php b/src/Routes/Collection.php index f6388c6..5bd8901 100644 --- a/src/Routes/Collection.php +++ b/src/Routes/Collection.php @@ -85,7 +85,9 @@ protected function getRouteInformation(Route $route, $filter, $namespace) break; } - return compact('host', 'methods', 'uri', 'name', 'action'); + $items = ['host', 'methods', 'uri', 'action', 'name']; + + return compact(array_diff($items, config('laroute.exclude'))); } }