直接从公共文件夹加载 HTML 文件作为 Laravel 中的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31863450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load an HTML file directly from public folder as View in Laravel
提问by cyber8200
I'm using Laravel. I'm trying to put together my own wedding invitation. I have a folder in my public folder, will styles, and scripts for it.
我正在使用 Laravel。我正在尝试整理自己的婚礼请柬。我的公用文件夹中有一个文件夹,它的样式和脚本。
I'm wondering if I can point to that folder, instead of copy-and-paste everything into a new blade file.
我想知道我是否可以指向该文件夹,而不是将所有内容复制并粘贴到新的刀片文件中。
Route
路线
Route::get('/wedding', 'WeddingController@index');
Route::get('/wedding', 'WeddingController@index');
WeddingController
婚礼控制器
<?php namespace App\Http\Controllers;
class WeddingController extends Controller {
public function index()
{
return view('wedding.index');
}
}
Question
题
I'm wondering if I can point my index function to load the index.htmlfrom one of my folder in my /publicfolder.
我想知道我是否可以指向我的索引函数index.html从我的/public文件夹中的一个文件夹加载。
Do we have to load the .bladefile from the app/resources/viewsdirectory ?
我们是否必须.blade从app/resources/views目录中加载文件?
Any helps / suggestions on this will be much appreciated.
对此的任何帮助/建议将不胜感激。
采纳答案by Ben Claar
Just place the wedding folder directly inside the public folder:
只需将婚礼文件夹直接放在公共文件夹中:
mv wedding/ /path/to/laravel/public
Then visit your site URL with a wedding suffix:
然后访问带有婚礼后缀的网站 URL:
http://my-site.com/wedding
This will load the index.htmlfrom inside the wedding folder.
这将从index.html婚礼文件夹中加载。
This works via Nginx's try_filesdirective in your /etc/nginx/sites-enabled/my-siteconfig file:
这通过配置文件中的Nginxtry_files指令起作用/etc/nginx/sites-enabled/my-site:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
This instructs Nginx to first search for an actual file corresponding to the URL (for example, /css/app.css, /wedding/index.html, etc). If it does not find a matching file (e.g. it would normally return a 404 not found), then it should instead pass the query string as an argument to the index.php script.
这会指示 Nginx 首先搜索与 URL 对应的实际文件(例如/css/app.css,/wedding/index.html、 等)。如果它没有找到匹配的文件(例如,它通常会返回 404 not found),那么它应该将查询字符串作为参数传递给 index.php 脚本。
回答by Ankit Jain
Load static files from controller using FileFacade
使用FileFacade从控制器加载静态文件
use File;
return File::get(public_path() . '/to new folder name/index.html');
回答by Ben Claar
One solution is to rename your wedding invitation's index.htmlto index.phpand place it within your resources/viewsfolder, so that it becomes a Laravel template.
一种解决方案是将您的婚礼邀请重命名index.html为index.php并将其放置在您的resources/views文件夹中,使其成为 Laravel 模板。
cd path/to/laravel
mkdir resources/views/wedding
mv public/wedding/index.html resources/views/wedding/index.php
Then you can call it from your controller as you wish:
然后您可以根据需要从控制器调用它:
public function index()
{
return view('wedding.index');
}
Of course with this method you'll have to ensure any CSS/JavaScript/image urls are properly mapped to their respective locations within the public/folder.
当然,使用此方法,您必须确保任何 CSS/JavaScript/图像 url 都正确映射到public/文件夹中的相应位置。

