@@ -17,16 +17,26 @@ public function __construct(Router $router)
1717 public function generate ()
1818 {
1919 $ json = (string ) $ this ->nameKeyedRoutes ();
20+ $ appUrl = rtrim (config ('app.url ' ), '/ ' ) . '/ ' ;
2021
2122 return <<<EOT
2223<script type="text/javascript">
23- var namedRoutes = JSON.parse(' $ json');
24+ var namedRoutes = JSON.parse(' $ json'),
25+ baseUrl = ' $ appUrl';
2426
25- function route (name, params) {
26- return namedRoutes[name].uri.replace(
27- /\{([^}]+)\}/,
27+ function route (name, params, absolute = true) {
28+ var domain = namedRoutes[name].domain ? namedRoutes[name].domain : baseUrl;
29+ var url = (absolute ? domain : '') + namedRoutes[name].uri
30+
31+ return url.replace(
32+ /\{([^}]+)\}/gi,
2833 function (tag) {
29- return params[tag.replace(/\{|\}/gi, '')];
34+ var key = tag.replace(/\{|\}/gi, '');
35+ if (params[key] === undefined) {
36+ console.error('Ziggy Error: "' + key + '" key is required for route "' + name + '"');
37+ return tag;
38+ }
39+ return params[key];
3040 }
3141 );
3242 }
@@ -38,7 +48,8 @@ public function nameKeyedRoutes()
3848 {
3949 return collect ($ this ->router ->getRoutes ()->getRoutesByName ())
4050 ->map (function ($ route ) {
41- return collect ($ route )->only (['uri ' , 'methods ' ]);
51+ return collect ($ route )->only (['uri ' , 'methods ' ])
52+ ->put ('domain ' , $ route ->domain ());
4253 });
4354 }
4455}
0 commit comments