简体   繁体   English

用于Laravel路由的Java语言中的全局变量-这是一个好主意吗?

[英]Global Variable in Javascript for Laravel Routes - Is this a good idea?

I've created some code using a View Composer where I am passing my Route Collection through to the front end on all views, so I can access all of my laravel routes in Vuejs via the route named associated with them. 我已经使用View Composer创建了一些代码,在其中我将我的Route Collection传递到了所有视图的前端,因此我可以通过与其关联的命名路由来访问Vuejs中的所有laravel路由。

For example, to upload an image using a vue component, instead of passing my upload route into the Vue Component, it is listed as a part of a global variable: 例如,要使用Vue组件上载图像,而不是将我的上传路线传递到Vue组件中,它会被列为全局变量的一部分:

var uploadRoute     = _.find(globalRoutes, function(route) { return route.name == 'route-name.image.upload' });
$.post(uploadRoute, data) ... etc

My question is...is this sensible? 我的问题是...这明智吗? I'm publically publishing my entire app's routes. 我正在公开发布整个应用的路线。

Thanks 谢谢

I think your hunch about exposing your entire apps routes is legit. 我认为您对暴露整个应用程序路线的直觉是合法的。 IMO you should explicitly pick out the routes that you need. IMO,您应该明确选择所需的路线。 So in thise case, you should only expose route-name.image.upload . 因此,在这种情况下,您只应公开route-name.image.upload You could create a tiny helper function to look up routes and output them along with the URL as JSON. 您可以创建一个小小的辅助函数来查找路由并将其与URL一起输出为JSON。

function json_routes(array $routes)
{
   $return = [];

   foreach($routes as $route)
   {
       $return[$route] = route($route);
   }

   return new \Illuminate\Support\HtmlString(json_encode($return));

}

And the, in your main view: 在您的主视图中:

var routes = {{ json_routes(["route-name.image.upload"]) }};

Getting a route is simple: 获得路线很简单:

routes['route-name.image.upload']; 

This is the most basic exaple I can think of. 这是我能想到的最基本的例子。 You can optimize it quite a bit. 您可以优化它很多。 Just some ideas: 只是一些想法:

  • Place the routes in a central place, fx. 将路线放置在中央位置fx。 a config element: json_routes(config('app.json_routes')) 一个配置元素: json_routes(config('app.json_routes'))
  • Build a command that generates a static .json file so that you don't iterate through the routes on each page load. 构建一个生成静态.json文件的命令,这样您就不必遍历每次页面加载时的路由。 Remember to re-generate when you add more routes. 当您添加更多路线时,请记住要重新生成。
  • Create a function instead of an object to get the route. 创建一个函数而不是对象来获取路线。 That allows you to build in logic and gives a more Laravel-like feel in your js: function route(path){ return window.routes.hasOwnProperty(path) ? window.routes[path] : null ;} 这使您可以构建逻辑并在js中提供更类似于Laravel的感觉: function route(path){ return window.routes.hasOwnProperty(path) ? window.routes[path] : null ;} function route(path){ return window.routes.hasOwnProperty(path) ? window.routes[path] : null ;}

  • (Advanced) Re-write Laravels router logic and hook into the options array, allowing you to do something like Route::get('dashboard', '...', ['as'=>'dashboard', 'expose'=>true]); (高级)重新编写Laravels路由器逻辑并连接到options数组,使您可以执行Route::get('dashboard', '...', ['as'=>'dashboard', 'expose'=>true]); , then dynamically generate the before mentioned json-file on all routes with the expose option. ,然后使用expose选项在所有路由上动态生成前面提到的json文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM