如何路由到Laravel中的静态文件夹

时间:2022-06-12 00:32:57

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页面?

3 个解决方案

#1


13  

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

关键是您的路径网址与公共文件夹之间没有冲突

#2


3  

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

在尝试访问公共文件/文件夹时,过去有一些错误创建无限重定向,但我认为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 ^(.*)/$ /$1 [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。

#3


2  

Use:

return Redirect::to('/test/index');

Either at the function at your routes file or inside a controller.

在路径文件的函数或控制器内部。

#1


13  

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

关键是您的路径网址与公共文件夹之间没有冲突

#2


3  

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

在尝试访问公共文件/文件夹时,过去有一些错误创建无限重定向,但我认为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 ^(.*)/$ /$1 [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。

#3


2  

Use:

return Redirect::to('/test/index');

Either at the function at your routes file or inside a controller.

在路径文件的函数或控制器内部。