【问题标题】:Laravel Blade EchoLaravel 刀片回声
【发布时间】:2017-01-02 03:21:21
【问题描述】:

我试图理解 laravel。 在我的测试中,我创建了一个这样的控制器:

    public function getDirectorySize( $path = null)
{ 
$path="/folder/"; 
$pathlog="/laravel.log"; 
$pathbackup="/folder/"; 
$ar=getDirectorySize($path); 

$totalsize = 0; 
$totalcount = 0; 
$dircount = 0; 
if ($handle = opendir ($path)) 
{ 
while (false !== ($file = readdir($handle))) 
{ 
  $nextpath = $path . '/' . $file; 
  if ($file != '.' && $file != '..' && !is_link ($nextpath)) 
  { 
    if (is_dir ($nextpath)) 
    { 
      $dircount++; 
      $result = getDirectorySize($nextpath); 
      $totalsize += $result['size']; 
      $totalcount += $result['count']; 
      $dircount += $result['dircount']; 
    } 
    elseif (is_file ($nextpath)) 
    { 
      $totalsize += filesize ($nextpath); 
      $totalcount++; 
    } 
  } 
} 
} 
closedir ($handle); 
$total['size'] = $totalsize; 
$total['count'] = $totalcount; 
$total['dircount'] = $dircount; 
return $total; 

} 

在我的刀片中,我想显示这样的数据:

        Details for the path : {{ $path }} 
        Details for the log : {{ $pathlog }}
        Details for the backup : {{ $pathbackup }} 
        Total size : {{ sizeFormat($ar['size']) }}
        No. of files : {{ $ar['count'] }} 
        <? echo "No. of directories : ".$ar['dircount']."<br>"; ?>

但我收到了这个错误:

Undefined variable: path

我想显示这个函数的任何信息

【问题讨论】:

  • 您不应该将这些变量发送到视图吗?
  • 我想在我的刀片视图中显示 $path :( 任何解决方案?
  • return view('template, ['path' =&gt; $path]);
  • 我已将此添加到我的控制器中:$this->data = array('path' => $path);但仍然无法正常工作

标签: laravel-5 laravel-blade


【解决方案1】:

你能试试这样的吗?

public function yourControllerFunction() {
    $path="/folder/"; 
    $pathlog="/laravel.log"; 
    ...
    // Add variables you want to pass to the view
    $data = array(
        'path' => $path,
        'pathlog' => $pathlog,
    )
    return view('view_file', $data);
}

然后在视图中你应该可以访问 $path 和 $pathlog。

【讨论】:

  • 我添加了这样的返回视图:return view('builder.config.log',$this->data);但仍然有同样的问题。
  • 我的示例中的 $data 变量是在控制器函数内部声明的,因此不需要 $this。你试过不使用 $this 吗?
猜你喜欢
  • 2020-01-01
  • 2017-01-10
  • 2018-05-06
  • 2018-09-02
  • 1970-01-01
  • 2019-01-31
  • 2019-04-02
  • 2013-11-22
  • 2017-01-05
相关资源
最近更新 更多