从Laravel中获取最准确的请求时间,用于前端。

huangapple go评论56阅读模式
英文:

Get the most accurate request time from Laravel to be used in the frontend

问题

I need to get an accurate timestamp of the request in order to then calculate the session end time.

I need the timestamp in the layout to be available globally for some JS script, so right now I store it in the layout file:

// layout.blade.php

<div id="request-timestamp" data-request-timestamp="{{ now()->timestamp }}"></div>

Then I can access it using JS:

const requestTimestamp = document.getElementById("request-timestamp").dataset.requestTimestamp;

But, is there a way to get a more accurate timestamp to the time where Laravel actually saves the session time? Because in my app, there are a few DB connections before it actually gets to the frontend, so it might be less accurate.

Can I somehow send the request timestamp to the layout file in a more accurate way?

英文:

I need to get an accurate timestamp of the request in order to then calculate the session end time

I need the timestamp in the layout to be available globally for some JS script, so right now I store it in the layout file:

// layout.blade.php

&lt;div id=&quot;request-timestamp&quot; data-request-timestamp=&quot;{{ now()-&gt;timestamp }}&quot;&gt;&lt;/div&gt;

Then I can access it using JS:

const requestTimestamp = document.getElementById(&quot;request-timestamp&quot;).dataset.requestTimestamp;

But, is there a way to get a more accurate timestamp to the time where Laravel actually saves the session time? Because in my app, there are a few DB connections before it actually gets to the frontend, so it might be less accurate.

Can I somehow send the request timestamp to the layout file in a more accurate way?

答案1

得分: 2

为确保时间戳尽可能接近服务器端的请求处理,您可以在控制器中获取它并将其直接传递给视图。

控制器

public function index()
{
    $requestTimestamp = now()->timestamp;
    
    return view('your_view', compact('requestTimestamp'));
}

脚本

<script>
    const requestTimestamp = {{ $requestTimestamp }};
</script>
英文:

To ensure that the timestamp is as close as possible to the server-side request processing, you can get it in the controller and pass it directly to the view.

Controller

public function index()
{
    $requestTimestamp = now()-&gt;timestamp;
    
    return view(&#39;your_view&#39;, compact(&#39;requestTimestamp&#39;));
}

Script

&lt;script&gt;
    const requestTimestamp = {{ $requestTimestamp }};
&lt;/script&gt;

答案2

得分: 0

我最终使用了在public/index.php文件中定义的LARAVEL_START常量,它返回Unix时间戳:

// public/index.php

define('LARAVEL_START', microtime(true));

然后将它设置在一个div元素中:

<div id="request-timestamp" data-request-timestamp="{{ LARAVEL_START }}"></div>

这个方法效果还不错。

英文:

I ended up using the LARAVEL_START constant that is defined in the public/index.php file, which returns the unix timestamp:

// public/index.php

define(&#39;LARAVEL_START&#39;, microtime(true));

And then set it in a div:

 &lt;div id=&quot;request-timestamp&quot; data-request-timestamp=&quot;{{ LARAVEL_START }}&quot;&gt;&lt;/div&gt;

That works pretty well.

huangapple
  • 本文由 发表于 2023年6月8日 23:35:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定