如何将Laravel视图转换为静态HTML并从缓存中提供它们?(How to convert Laravel views to static HTML and serve them from cach

编程入门 行业动态 更新时间:2024-10-05 05:16:32
如何将Laravel视图转换为静态HTML并从缓存中提供它们?(How to convert Laravel views to static HTML and serve them from cache?)

我想设置功能,将Laravel 5.3中的视图转换为静态HTML,并将其提供给访客,直到CMS调用指定。 我还想使用HTTP标头来缓存访问者计算机上的页面。

如何在启用了路由缓存的情况下将视图作为静态HTML提供? 如何使用HTTP标头从用户缓存中提供静态HTML视图? 是否可以/应该使用其他任何警告或性能提升技巧?

I would like to setup functionality to transform the views in Laravel 5.3 to static HTML and serve it to visitors untill specified from a CMS call. I also like to use HTTP headers to cache the pages on a visitors computer.

How can i get views to be served as static HTML with route caching enabled? How can i serve the static HTML views from the users cache using HTTP headers? Are there any other caveats or performance boosting tricks i can/should utilise?

最满意答案

在这里,我将使用代码回答File based cache 。 这不完全是HTML缓存,但表现良好。

看看我的Laravel Installation / var / www / ea(这是我的laravel Instalaltion目录)

在存储/框架/缓存内部,您将找到缓存的文件

Step 1:如何创建缓存

Step 2:此步骤在Step 2:之前,即configure缓存到file缓存。 打开config/cache.php并检查两件事

'default' => env('CACHE_DRIVER', 'file'), //这意味着您的缓存默认驱动程序是基于文件的。

然后'path' => storage_path('framework/cache'), //这是我的屏幕截图在上面显示的路径。

第3步:让我们编写代码来创建缓存。 打开Controller方法并编写以下内容

\Cache::put( 'cachekey', 'Hello I am Abdul', 1 ); // 1分钟

缓存的laravel文档

第4步:检查在framework/cache/创建的任何新目录

那是你的缓存文件。

//在创建之前检查缓存键是否已经存在,否则创建缓存文件if (Cache::has('key')){ Cache::get('key'); } else { Cache::put('key', $values, 10); } if (Cache::has('key')){ Cache::get('key'); } else { Cache::put('key', $values, 10); }

Here I will answer the File based cache with the code. Which is not exactly the HTML cache, but performs well.

Look at my Laravel Installation /var/www/ea (This is my Laravel Installation directory)

Inside storage/framework/cache you will find the cached files

Step 1: How to create cache

Step 2: This step comes before step 1, which is configure cache to file cache. Open the config/cache.php and do check two things

'default' => env('CACHE_DRIVER', 'file'), //It means your cache default driver is File based.

Then

'path' => storage_path('framework/cache'), //This is the path My screenshot is showing above.

Step 3: Lets write the code to create cache. Open your Controller method and write following

\Cache::put( 'cachekey', 'Hello I am Abdul', 1 ); // 1- minute

laravel documentation for cache

Step 4: Check for any new directory created inside framework/cache/

That's your cache file.

// Following code check if the cache key already exists before creating, // otherwise create cache file if (Cache::has('key')){ Cache::get('key'); } else { Cache::put('key', $values, 10); }

更多推荐

HTML,HTTP,视图,views,缓存,电脑培训,计算机培训,IT培训"/> <meta name="descrip

本文发布于:2023-07-09 10:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   转换为   缓存   并从   如何将

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!