php - 将类公共属性传递到视图文件并使用 $this 访问它们
问题描述
有没有办法使用$this从视图文件中访问 Controller 和 ChildController 的所有公共属性
<?PHP
class Controller {
public $userFamilyName = "Doe";
}
class ChildController extends Controller{
public $user = "John"
public function routeFunction(){
return view("viewFile")
}
}
查看文件 => viewFile.blade.php
<header>Hello {$this->user} {$this->userFamilyName} !</header>
解决方案
您可以使用View::share
$this 使您的视图文件中的属性可用
class ChildController extends Controller{
public $user = "John"
public function __construct()
{
\View::share('user', $this->user);
\View::share('userFamilyName', $this->userFamilyName);
}
public function routeFunction(){
return view("viewFile")
}
}
现在可以在刀片中使用{{ $this->user }}
.
阅读有关在视图中共享数据的更多信息: https ://laravel.com/docs/5.8/views#sharing-data-with-all-views
推荐阅读
- java - 如何在 vaadin-flow 中正确指定对话框大小
- haskell - 关于并行性和 `do` 的问题
- jira - 问题状态更改时自动合并 PR
- windows - 如何将 cmd 输出保存到已创建的文件夹或目录中?
- java - 连接移动网络时如何获取连接速度?
- php - 从php中的时差总和获取总小时和分钟
- c# - 尝试将 .netBrowser 发布请求转换为 Cefsharp Chromium
- javascript - 以所有函数都可以访问它的方式以角度声明变量
- angularjs - 如何从 EmployeeDataList 列表中选择基于 EmployeeNumber 的单选按钮
- python - Python logger:限制日志文件似乎没有限制我的文件