为什么我们要创建软连接?

因为项目根目录下的 public 是一个特殊的目录,这个目录下存放可公共访问的资源。

什么是可公共访问呢?就是可以通过`http://域名/文件路径`的形式访问到。

像我们通过laravel上传文件,如果我们把上传的资源文件存储在本地的话,Laravel 默认会放在 storage/app 里面,但是这个目录是不可见的,如果想要能公共访问就必须暴露在项目根目录下的 public 中,这就是创建软链接的原因 。

我们执行php artisan storage:link这个命令后呢,项目中public目录下就会多出来一个storage目录,这个目录就是storage/app/public目录的一个软连接。

简单举例:如果我们的域名是http://www.zhangzhengshan.com

项目的目录结构如下:

storage

—-app

——–public

————avatar.png

public

—-storage

我们通过访问http://www.zhangzhengshan.com/storage/avatar.png就可以访问到storage/app/public/avatar.png这个文件了。

//前台代码如下:

    <div class="layui-card-body">    
    <form class="layui-form" action="{{route('admin.news.store')}}" method="post" enctype="multipart/form-data">              
    {{csrf_field()}}
    <?php
    $category = \App\Services\NewsService::category();
    ?>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">分类</label>
            <div class="layui-input-block" style="width: 100%;">
                <select name="news[category_id]" lay-verify="required">
                    <option value="">请选择资讯类型</option>
                    <?php foreach ($category as $k=>$v){ ?>
                    <option value="{{$v->id}}" @if(isset($news->category_id) && $news->category_id == $v->id) selected @endif>{{$v->title}}</option>
                    <?php } ?>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block" style="width: 100%;">
                @if(isset($news->title))
                    <input type="text"  name="news[title]" value="{{ $news->title }}"  lay-verify="required" placeholder="请输入资讯标题" class="layui-input"  style="width: 500px;">
                @else
                    <input type="text"  name="news[title]" value="" lay-verify="required" placeholder="请输入资讯标题" class="layui-input"  style="width: 500px;">
                @endif
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-block" style="width: 100%;">
                @if(isset($news->picture))
                    <input type="file" name="pic_file" value="" id="file">
                    <img src="{{ $news->picture }}">
                @else
                    <input type="file" name="pic_file" id="file">
                @endif
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline" style="width: 100%;">
            <label class="layui-form-label">简介</label>
            <div class="layui-input-block">
                @if(isset($news->desc))
                    <textarea  type="text" id="desc"  name="news[desc]" rows="10" cols="100">{{ $news->desc }}</textarea>
                @else
                    <textarea  type="text" id="desc"  name="news[desc]" rows="10" cols="100"></textarea>
                @endif
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline" style="width: 80%;">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                @if(isset($news->content))
                    <textarea  type="text" id="content"  name="news[content]" style="display: none;">{{ $news->content }}</textarea>
                @else
                    <textarea  type="text"  id="content"  name="news[content]" style="display: none;"></textarea>
                @endif
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block" style="width: 100%;">
                <input type="checkbox" @if(isset($news->status) && $news->status == 1) checked @endif name="news[status]" lay-skin="switch" lay-text="开启|关闭">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="formDemo">确 认</button>
            <a  class="layui-btn" href="{{route('admin.news.index')}}" >返 回</a>
        </div>
    </div>
    @section('script')
        <script>
            layui.use('layedit', function(){
                var layedit = layui.layedit;
                layedit.build('content',{
                    height: 280, //设置编辑器高度
                }); //建立编辑器
            });
        </script>
    @endsection
    </form>
   </div>

//上传图片

    public function store(Request $request){
          $data = $request->get('news');
          $data['status'] = (isset($data['status']) && $data['status'] =='on'?1 : 0);
          //不为空
          if($request->hasFile('pic_file')){
               $file = $request->file('pic_file');
               $fileExtension = $file->getClientOriginalExtension();
               if(! in_array($fileExtension, ['jpg', 'png','jpeg','gif'])) {
                    return false;
               }
               //临时绝对路径
               $filePath = $file->getRealPath();
               $filename = date('Ymdhis').mt_rand(10,99).'.'.$fileExtension;
               Storage::disk('public')->put($filename, file_get_contents($filePath));
               $data['picture'] = config('app.url').'/storage/'.$filename;
          }
          if (News::create($data)){
               return redirect()->to(route('admin.news.index'))->with(['status'=>'添加公司成功']);
          }
          return redirect()->to(route('admin.news.index'))->withErrors('系统错误');
    }

//控制台执行php artisan storage:link,把/public/storage指向storage/app/public,这样就可以访问上传到storage/app/public里面的图片了