【问题标题】:Laravel Passing Controller data to ViewLaravel 将控制器数据传递给视图
【发布时间】:2017-05-30 20:12:19
【问题描述】:

试图将控制器中生成的数据传递给视图。

web.php 中的路由

Route::get('person', ['uses'=>'PersonController@index']);

控制器应用程序/Http/Controllers/Person.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PersonController extends Controller
{
  public function index()
  {

    $age = 35;
    $height = 68;
    $weight = 102;

    // Calculate rest of BPX values
    $BPXVals = [];
    // Actual Weight
    $BPXVals['actualWeight']          =   $weight;
    $BPXVals['actualWeight2']         =   pow($weight, 2);
    $BPXVals['actualWeight3']         =   pow($weight, 3);
    $BPXVals['weightLN']              =   log($weight);

    return View::make('person.index', ['BPXVals'=>$BPXVals]);

  }
}

在 Resources/Views/index.php 中查看

<!doctype html>
<html>
    <head>
    </head>
    <body>
      {{  $BPXVals  }}
    </body>
</html>

得到以下错误:

ReflectionException in Container.php line 721:
Class App\Http\Controllers\PersonController does not exist

我在定义路由或将路由链接到控制器和/或视图时做错了什么?

【问题讨论】:

  • 在项目根目录的控制台中尝试composer dump-autoload,因为我没有在代码中看到任何错误

标签: laravel-5


【解决方案1】:

尝试将app/Http/Controllers/Person.php改成app/Http/Controllers/PersonController.php

【讨论】:

  • Thx,我将控制器文件的名称更改为 PersonControoler.php,现在出现以下错误:helpers.php 第 533 行中的 ErrorException:htmlspecialchars() 期望参数 1 为字符串,给定数组 (查看:/path/to/resources/views/person/index.blade.php)
  • 您不能像 {{ $BPXVals }} 那样打印数组。您需要将值打印为{{ $BPXVals['weightLN'] }}
  • 成功了!所以我需要在视图中引用数组索引,不能像在 Twig 中那样转储数组。这就是我需要知道的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-29
  • 2018-03-20
  • 2019-12-26
相关资源
最近更新 更多