繁体   English   中英

刀片视图中的未定义变量错误

[英]undefined variable error in blade view

我目前正在使用Laravel 5.2,尝试显示当前存储在“ 存储”文件夹中的单击图像。 我试图在刀片视图中显示这些图像,但是每次加载页面时,都会出现未定义的变量异常。

控制器

public function createemoji($action,$statusId)
{  
    $path = storage_path('app/public/images/'.$action.'.gif');

    /*$request=new storage();
    $request->comment=$path;
    $request->user_id=Auth::user()->id;
    $request->post_id=$statusId;
    $request->save();*/
    return redirect()->returnemoji()->with('file'->$path);

}

public function returnemoji($file)
{           
    return Image::get('$file')->response();
}

在我的默认视图中,我尝试使用count(),但是每次加载页面时,都会给我Undefined variable 我应该如何显示?

尝试更改此:

->with('file'->$path);

对此:

->with('file', $path);

https://laravel.com/docs/5.3/views#passing-data-to-views

我认为您必须尝试以下方法:

代替:

return redirect()->returnemoji()->with('file'->$path);

尝试这个:

return redirect()->returnemoji($path);

是的,从中删除引号:

return Image::get('$file')->response();

With函数带有两个参数key和value

你可以用这个

return redirect()->returnemoji()->with('file',$path);

您可以尝试以下方法:

代替:

return redirect()->returnemoji()->with('file'->$path);

尝试这个:

return $this->returnemoji($path);

希望这对您有帮助。

有一些问题。

  1. 单引号不处理变量,所以代替此

     return Image::get('$file')->response(); 

    你可以这样做

     return Image::get("$file")->response(); 

    要么

     return Image::get("{$file}")->response(); 

    但这并不是必需的,因为您仅使用变量本身而无需任何其他格式,因此请完全删除引号

     return Image::get($file)->response(); 
  2. 对象运算符->在对象范围内用于访问对象的方法和属性。 您的函数returnemoji()不是RedirectResponse类的方法,而这是redirect()帮助器方法所返回的。

  3. with()方法在这里不合适,您只需要将参数传递给这样的函数

     return redirect()->returnemoji($path); 

可选地,我建议遵循PSR2代码样式标准 ,其中包括驼峰式变量名,因此createemoji()应该是createEmoji() 我也认为在Laravel中返回大多数数据类型时,通常可以省略response() ,因为它将为您自动处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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