好的,
这是UserController
的index()
方法
public function index()
{
$name = 'echoashu';
return view('home', compact('name'));
}
就这么简单,这里我的home.blade.php
代码
<span class="info-box-number">{{$name}} </span>
这必须按照文档理想地工作,但是它返回未定义的变量error
Undefined variable: name (View: C:'xampp'htdocs'laravel1'resources'views'home.blade.php)
猜吗? ?
给人一条鱼,你喂他一天;授人以渔,授人以渔
下面让我来说明在这种情况下如何调试(fish)
第一步:确保你的呼叫是正确的
你可以通过
Route::get('yourcall', 'UserController@index');
在将其传递到视图中之前,只需在控制器中打印一些内容,例如
public function index()
{
echo 'Whao ! I landed correctly';
}
第2步:
确保你看到了你所说的
现在返回到视图,确保您的视图存在并且具有扩展名为yourview.blade.php
你可以通过
return view('yourview', compact($YourValue));
你应该有一个名为yourview.blade.php
在叶片内部,您可以获得传递的值,如
{{$YourValue}} // If you have your file name as yourview.blade.php
或
<?php
echo $YourValue // If you have your file name as yourview.php
?>