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

时间:2016-09-29 16:37:16

标签: php laravel laravel-5.2 blade

我目前正在使用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(),但每次加载页面时,都会显示未定义变量。我该如何展示它?

5 个答案:

答案 0 :(得分:2)

尝试改变这一点:

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

对此:

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

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

答案 1 :(得分:0)

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

而不是:

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

试试这个:

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

是的,删除引号:

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

答案 2 :(得分:0)

使用函数需要两个参数键和值

您可以使用此

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

答案 3 :(得分:0)

你可以尝试一下:

而不是:

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

试试这个:

return $this->returnemoji($path);

希望这会对你有所帮助。

答案 4 :(得分:0)

有一些问题。

  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);
    
  4. 或者,我建议您遵循PSR2 code style standard,其中包含驼峰式变量名称,因此createemoji()应为createEmoji()。另外我认为在Laravel中返回大多数数据类型时通常可以省略response(),因为它会自动为您处理。