Laravel elFinder图片上传问题

时间:2017-08-22 00:59:42

标签: php laravel

我在我的应用上使用this package上传图片和文件,在本地工作得很好但是因为我将我的应用程序移动到服务器我收到此错误:

  

在null上调用成员函数hasPermissionTo()   在AdminMiddleware.php(第22行)

我在Laravel 5.4中使用TinyMCE 4

这是我的AdminMiddleware

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use App\User;

class AdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
      $user = User::all()->count();
      if (!($user == 1)) {
          if (!Auth::user()->hasPermissionTo('Administer roles & permissions')) //If user does //not have this permission
      {
              abort('401');
          }
      }

      return $next($request);
    }
}

这是我在post create page

上的脚本
@section('scripts')
<script type="text/javascript" src="{{asset('js/select2.min.js')}}"></script>
<script>
  var editor_config = {
    path_absolute : "/",
    selector: "textarea.my-editor",
    plugins: [
      "advlist autolink lists link image charmap print preview hr anchor pagebreak",
      "searchreplace wordcount visualblocks visualchars code fullscreen",
      "insertdatetime media nonbreaking save table contextmenu directionality",
      "emoticons template paste textcolor colorpicker textpattern codesample"
    ],
    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media | emoticons charmap | code codesample | forecolor backcolor",
    relative_urls: false,
    remove_script_host: false,
    file_browser_callback : function(field_name, url, type, win) {
      var x = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
      var y = window.innerHeight|| document.documentElement.clientHeight|| document.getElementsByTagName('body')[0].clientHeight;

      var cmsURL = editor_config.path_absolute + 'laravel-filemanager?field_name=' + field_name;
      if (type == 'image') {
        cmsURL = cmsURL + "&type=Images";
      } else {
        cmsURL = cmsURL + "&type=Files";
      }

      tinymce.activeEditor.windowManager.open({
        file: '<?= route('elfinder.tinymce4') ?>',// use an absolute path!
        title: 'File Manager',
        width: 900,
        height: 450,
        resizable: 'yes'
      }, {
        setUrl: function (url) {
          win.document.getElementById(field_name).value = url;
        }
      });
    }
  };

  tinymce.init(editor_config);
</script>
<script>
  {!! \File::get(base_path('vendor/barryvdh/laravel-elfinder/resources/assets/js/standalonepopup.js')) !!}
</script>
@endsection

有谁知道为什么我有这个问题以及如何解决它?

error image

2 个答案:

答案 0 :(得分:0)

如果您使用Auth::user()并且没有任何用户登录,那么您需要在空对象上调用方法,因此必须先检查用户是否已登录。您可以修复它通过使用之前的Auth::check()方法。

快速举例:

if(Auth::check()){
  //we have an user logged, let's check their permissions
  if (!Auth::user()->hasPermissionTo('Administer roles & permissions')) {

  }
}else{
        // there aren't any user logged in
}

答案 1 :(得分:0)

<强>解决

我在public_html

的包文件夹中上传了视图文件

并且elfinder middelware也变成了数组:

'middleware' => array('web', 'Auth'),

现在一切正常。