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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:36:53  来源:igfitidea点击:

Undefined variable: request

laravel

提问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 $requestparameter 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

You can also use request()helper function as:

您还可以将request()辅助函数用作:

if(request()->hasFile('file')) {
    ...
}

The request function returns the current request instance.

request 函数返回当前的请求实例。

回答by Erin

Use Request::hasFileinstead 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 $requestinjected in your method (I think, you have only posted part of your method). Check this and add it as parameter if necessary.

显然$request,您的方法中没有注入(我认为,您只发布了部分方法)。检查此项并在必要时将其添加为参数。