简体   繁体   中英

laravel prevent unauthenticated users from viewing the files

I want to try to make some route filter using pattern filter, but it's doesn't work. How to make a route to prevent unauthenticated users from viewing files on specific folder ?

Try this:

Route::get('/directory/{file}', array('before' => 'auth', function($file)
{
    return public_path() . "/directory/$file";
}));

Change directory to whatever the directory is you are trying to protect. Also, I assumed the files you want to deal out are in your public directory. That may need to be changed too, depending on your usage.

Auth is a filter that is already created in a default install of Laravel. It just makes sure they aren't a "guest." It will only let people access the directory if they are "logged in." I believe it looks for a cookie that Laravel sets when you log someone in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM