Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an configurable option for the secureUrl #52

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
14 changes: 12 additions & 2 deletions config/laroute.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

return [

/*
* The destination path for the javascript file.
*/
Expand All @@ -25,6 +24,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
*
Expand All @@ -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' => [],
];
5 changes: 3 additions & 2 deletions src/Console/Commands/LarouteGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function fire()
);

$this->info("Created: {$filePath}");
} catch (\Exception $e) {
} catch (\Exception $e){
$this->error($e->getMessage());
}
}
Expand All @@ -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');
}


Expand Down
4 changes: 3 additions & 1 deletion src/Routes/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}

}
41 changes: 28 additions & 13 deletions src/templates/laroute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
}
};

Expand Down Expand Up @@ -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 = {}])
Expand Down
2 changes: 1 addition & 1 deletion src/templates/laroute.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.