laravel 用php代码登录后用php下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55452968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download file with php after login with php code
提问by Tommy Tang
i have two links one for login (www.xxx/login/) one for download file (www.xxx/download/)
我有两个链接,一个用于登录(www.xxx/login/),一个用于下载文件(www.xxx/download/)
if already login, link to www.xxx/download/ will download file automatically, if not the page will redirect to www.xxx/login/
如果已经登录,链接到 www.xxx/download/ 将自动下载文件,否则页面将重定向到 www.xxx/login/
So how can i write the php code? to login first then go to download file
那么如何编写php代码呢?先登录然后去下载文件
回答by Lakhwinder Singh
If you are working on Laravel provided login functionality then this is default provided by Laravel.
如果您正在使用 Laravel 提供的登录功能,那么这是 Laravel 默认提供的。
You can check this here
你可以在这里查看
Illuminate\Foundation\Auth\AuthenticatesUsers
This is the trait
and check function sendLoginResponse
. Below is the code written in sendLoginResponse
function:-
这就是trait
检查功能sendLoginResponse
。下面是写在sendLoginResponse
函数中的代码:-
/**
* Send the response after the user was authenticated.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
If you are using custom login functionality follow this.
如果您使用自定义登录功能,请遵循此操作。
回答by Manojkiran.A
you just need the middleware
你只需要中间件
so add the _construct
method in top of controller
所以_construct
在控制器顶部添加方法
public function __construct()
{
$this->middleware('auth');
}
public function dowloadMethod($fileName)
{
//your code
}
if You are using the Closure
method in route
如果您Closure
在路由中使用该方法
Route::get('YourdowloadUrl/{fileName}',function($fileName)
{
//yourcode goesrhere
}
)->middleware(['web', 'auth']);