关于 Laravel 路由与 public 下的文件或文件夹的优先级问题

1、使用laravel命令行:

php artisan storage:link

为storage建立一个软链接,将上传至stroage/app/public下的图片文件对外暴露,可以使用http://xxx.xx/storage/文件名.jpg来访问。
2、建立控制器

namespace App\Http\Controllers;

class TestController extends Controller
{
    public function test3($id)
    {
        return "TestController@test3=>" . $id;
    }
}

3、在web.php中建立路由:

Route::get("/storage/{id}",'TestController@test3');

情况一:
访问时:http://local.api.laravel.com/storage/020.j...
时显示:(020.jpg文件不存在)
TestController@test3=>020.jpg
情况二:
访问时:http://local.api.laravel.com/storage/021.j...
时显示图片(021.jpg是一个确实存在的文件)

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 2
nfangxu

关于这个优先级, 是在 nginx 或者 Apache 的转发时判断的, nginx或者Apache会尝试寻找这个对应的文件, 当文件不存在的时候, 才会去将这个路径转发给 index.php

nginx配置

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

Apache配置

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
4年前 评论

是了。我的laravel就配置了.htaccess文件。
多谢回复!学习了。
baidu一下!找到如下资料

.htaccess都可以做什么

.htaccess文件可以的事情,主要包括:文件夹密码保护、用户自定义重定向、自定义404页面、扩展名伪静态化、禁止特定IP地址的用户、只允许特定IP地址的用户、禁止目录列表,等等。

.htaccess语法规则

RewriteEngine On //RewriteEngine 用于开启或停用rewrite功能。
RewiteBase /  //设定基准目录,例如希望对根目录下的文件rewrtie,就是"/"
RewriteCond %{HTTP_REFERER} !^http://(.+.)?baidu.com/ [NC]        //如果来源网址不是*.baidu.com
RewriteCond %{HTTP_REFERER} !^$                                   //并且来源网址不是空
RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]     //如果访问的文件后缀是jpeg,jpg,gif,bmp,png的图片,则重定向到一个固定的图片

1、图片重定向

RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$    //如果域名是localhost
RewriteCond %{REQUEST_FILENAME} !-f     //并且访问的文件找不到
RewriteRule ^images/(.+) http://127.0.0.1/test/showimages/$1 [R=302,L]     //则跳转到另一个域名下的另一个目录访问这个图片

2、二级域名重定向到www.yourdomain.com

rewriteEngine on
rewriteCond %{http_host} ^yourdomain.com [NC]
rewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

3、网站升级时,临时错误页面

RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$     //如果请求的地址不是maintenance.html
RewriteCond %{REMOTE_ADDR} !^123.123.123.123       //客户端ip如果不是这个
RewriteRule $ /error.html [R=302,L]                //则重定向到error.html这个升级提醒页面

4、重定向RSS地址到FeedSky

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !FeedSky [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feed.feedsky.com/yours

5、防止目录浏览:

Options All -Indexes

6、404重定向

ErrorDocument 404 /404.html

7、设置目录默认页面

DirectoryIndex about.html

感谢https://www.cnblogs.com/itshark/p/5849750.html的分享

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!