diff --git a/config/laroute.php b/config/laroute.php index 6866c49..122aa95 100644 --- a/config/laroute.php +++ b/config/laroute.php @@ -1,7 +1,6 @@ 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 * @@ -48,11 +53,16 @@ * with them. */ 'template' => '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/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/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'))); } } diff --git a/src/templates/laroute.js b/src/templates/laroute.js index f868098..8d09007 100644 --- a/src/templates/laroute.js +++ b/src/templates/laroute.js @@ -5,18 +5,19 @@ var routes = { absolute: $ABSOLUTE$, + secureUrl: $SECUREURL$, rootUrl: '$ROOTURL$', 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) { @@ -27,21 +28,26 @@ 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 "//" + route.host + "/" + uri + qs; + var path = this.getCorrectUrl(uri + qs); + if(this.absolute || forceAbsolute || this.isOtherHost(route)){ + return this.getCorrectAbsoluteUrl(path, route, forceAbsolute); } - return this.getCorrectUrl(uri + qs); + return path; }, isOtherHost: function (route){ 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)) { @@ -90,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; } }; @@ -132,13 +140,20 @@ 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, false, true); + }, // Generate a fully qualified URL to the given path. // $NAMESPACE$.route('url', [params = {}]) diff --git a/src/templates/laroute.min.js b/src/templates/laroute.min.js index 2593912..743b969 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,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