Laravel View Composer“未定义常量的使用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22466779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel View Composer "Use of undefined constant"
提问by Ryk Waters
been looking all evening and it all seems so easy and simple but just won't work!
整个晚上都在寻找,这一切看起来都很简单,但就是行不通!
Just starting out and trying to get a variable into a view.
刚刚开始并试图将变量放入视图中。
Whatever i do, i can't seem to read it.
无论我做什么,我似乎都无法阅读它。
routes.php:
路线.php:
Route::get('/', function()
{
return View::make('dashboard');
});
View::composer('dashboard', function($view) {
$view->with('links', "something");
});
dashboard.blade.php:
仪表板.blade.php:
@extends('base_view')
@section('content')
All the stuff!
{{links}}
@stop
base_view.blade.php
base_view.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel PHP Framework</title>
</head>
<body>
<div>TeamPro</div>
<div>@yield('content')</div>
</body>
</html>
Please tell me i'm just doing something stupid!
请告诉我我只是在做一些愚蠢的事情!
回答by Joseph Silber
Use $links
, not links
:
使用$links
,而不是links
:
@section('content')
All the stuff!
{{ $links }}
@stop
Every variable in PHP should be preceded by a $
sign. If there's no $
, PHP treats it as a constant.
PHP 中的每个变量都应该以$
符号开头。如果没有$
,PHP 会将其视为常量。
回答by duellsy
It's just a simple syntax issue, everything you send to a view is a php variable so still needs to be referenced with the $
这只是一个简单的语法问题,您发送到视图的所有内容都是一个 php 变量,因此仍然需要使用 $
In your view, you are referencing links
without the dollar sign.
在您看来,您是在links
没有美元符号的情况下进行引用。
If you update it to:
如果您将其更新为:
{{ $links }}
You'll be A-OK
你会没事的