restrict access to directory in public folder of laravel for Authenticated users only

后端 未结 1 1177
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 19:54

Suppose we have a Forms directory in public folder of laravel.

And Now I want to only Authenticated users can access to that and if an unautho

1条回答
  •  萌比男神i
    2021-01-21 20:48

    i would say you need to use local disk instead of public folder if you want to hide content from the user. take a look at here https://laravel.com/docs/5.3/filesystem

    still if you want to restrict the directory you can use wildcard route

    for ex.

    Route::get('/forms/{path?}', function () {
        if(!auth()->user()) {
            return 'unauthorized';
        }
        // if authenticated try to look for the file with requested path and return its content
        return File::get(public_path(ltrim($_SERVER['REQUEST_URI'],'/')));
    })->where(['path' => '.*']);
    

    0 讨论(0)
提交回复
热议问题