php 如何路由到 Laravel 中的静态文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29217937/
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
How to route to a static folder in Laravel
提问by ChiCgi
I have a folder I've placed in the /public folder in my Laravel site. The path is:
我有一个文件夹放在我的 Laravel 站点的 /public 文件夹中。路径是:
/public/test
the "test" folder has a file called index.html that is just a static resource I want to load from the public folder (I don't want to create a view for this within the framework--too much to explain).
“test”文件夹有一个名为 index.html 的文件,它只是一个我想从公共文件夹加载的静态资源(我不想在框架内为此创建一个视图——太多解释)。
I've made a route like this:
我做了这样的路线:
Route::get('/test', function() {
return File::get(public_path() . '/test/index.html');
});
I'm not able to get this to work. I just get an error from Chrome saying this page has a redirect loop.
我无法让它发挥作用。我刚从 Chrome 收到一条错误消息,说这个页面有一个重定向循环。
I can access it this way:
我可以通过这种方式访问它:
http://www.example.com/test/index.html
But I can't use the .html extension anywhere. The file must be visible as:
但我不能在任何地方使用 .html 扩展名。该文件必须可见:
http://www.example.com/test/
How can I serve a single HTML page from the Laravel public folder without having to use the .html extension?
如何在不使用 .html 扩展名的情况下从 Laravel 公共文件夹提供单个 HTML 页面?
采纳答案by terry low
used to face this problem before, as a dirty little trick, you may rename the test folder to something else then update
以前遇到过这个问题,作为一个肮脏的小技巧,您可以将测试文件夹重命名为其他内容然后更新
return File::get(public_path() . '/to new folder name/index.html');
the key is no conflict between your route url with your folder in public
关键是您的路由 url 与您的公共文件夹之间没有冲突
回答by Dallin
For a static file, I think you're wasting resources serving it through Laravel.
对于静态文件,我认为您正在浪费通过 Laravel 为其提供服务的资源。
There have been some bugs in the past creating infinite redirects when trying to access files/folders within public
, but I think the latest .htaccess that comes with Laravel has these resolved (on Apache, at least). It looks like this
过去在尝试访问public
.htaccess 中的文件/文件夹时会产生无限重定向,但我认为 Laravel 附带的最新 .htaccess 已经解决了这些问题(至少在 Apache 上)。看起来像这样
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
It says 'If the request is not for an existing folder, strip the trailing slash if there is one. If the request is not for an existing folder or for an existing file, load Laravel's index.php'
它说'如果请求不是针对现有文件夹的,则删除尾部斜杠(如果有)。如果请求不是针对现有文件夹或现有文件,则加载 Laravel 的 index.php'
I then added a .htaccess file within the subdirectory to ensure that the DirectoryIndex is set so that index.html will be loaded by default on a request for a directory.
然后,我在子目录中添加了一个 .htaccess 文件,以确保设置 DirectoryIndex,以便在请求目录时默认加载 index.html。
回答by danb4r
Use:
用:
return Redirect::to('/test/index');
Either at the function at your routes file or inside a controller.
无论是在路由文件中的函数中还是在控制器中。