laravel 未定义变量:请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41630393/
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
Undefined variable: request
提问by Rock
When I tried to upload the file it shows error:
当我尝试上传文件时,它显示错误:
Undefined variable: request
未定义变量:请求
This is where I used it: UploadController:
这是我使用它的地方:UploadController:
if($request->hasFile('file')){
$file = $request ->file('file');
$fileName = $file->getClientOriginalName();
$destinationPath = config('app.fileDesinationPath').'/'.$fileName;
$uploads = Storage::put($destinationPath,file_get_contents($file->getRealPath()));
}
return redirect()->to('/upload');
What's wrong here?
这里有什么问题?
回答by aceraven777
Add Request $request
parameter in your function. Example:
Request $request
在您的函数中添加参数。例子:
public function yourFunction(Request $request)
{
if($request->hasFile('file')){
$file = $request ->file('file');
$fileName = $file->getClientOriginalName();
$destinationPath = config('app.fileDesinationPath').'/'.$fileName;
$uploads = Storage::put($destinationPath,file_get_contents($file->getRealPath()));
}
return redirect()->to('/upload');
}
Please read the documentation thoroughly: http://laravel.com/docs
请仔细阅读文档:http: //laravel.com/docs
You can also watch Laravel tutorials here: http://laracasts.com
你也可以在这里观看 Laravel 教程:http://laracasts.com
回答by Amit Gupta
回答by Erin
Use Request::hasFile
instead of $request
. Example:
使用Request::hasFile
代替$request
。例子:
if(Request::hasFile('file')){
$file = Request::file('file');
$fileName = $file->getClientOriginalName();
$destinationPath = config('app.fileDesinationPath').'/'.$fileName;
$uploads = Storage::put($destinationPath,file_get_contents($file->getRealPath()));
}
return redirect()->to('/upload'); `
回答by Frank Provost
To access the $request variable you'll need to add it to your methods paramters
要访问 $request 变量,您需要将其添加到您的方法参数中
public function myFunction(Request $request)
{
// access $request here
}
In order to make this work you'll need to add
为了使这项工作,您需要添加
use Illuminate\Http\Request;
If you like to validate and authorize in your request you can create your own request instead of using Illuminates Request Class directly just use
如果您想在您的请求中验证和授权,您可以创建自己的请求,而不是直接使用 Illuminates 请求类,只需使用
php artisan make:request MyRequest
You'll find and authorize method and a part to return validation rules.
您将找到并授权方法和返回验证规则的部分。
回答by Loek
Apparently there isn't a $request
injected in your method (I think, you have only posted part of your method). Check this and add it as parameter if necessary.
显然$request
,您的方法中没有注入(我认为,您只发布了部分方法)。检查此项并在必要时将其添加为参数。