Php 将特定于控制器的数组传递给视图
要求 我正在用Laravel创建大量表单。因此,为了简单起见,我正在尝试实现一个类似于下面的方法,希望这能帮助我减少时间Php 将特定于控制器的数组传递给视图,php,arrays,laravel,variables,Php,Arrays,Laravel,Variables,要求 我正在用Laravel创建大量表单。因此,为了简单起见,我正在尝试实现一个类似于下面的方法,希望这能帮助我减少时间 定义私有变量。这些变量对于控制器中的所有函数都是通用的 将默认预定义数组传递给该控制器中的所有视图 这就是我试图实现的目标 //defining default variables for the page. Just changing this variable will enough for this controller private $titleForUpdat
//defining default variables for the page. Just changing this variable will enough for this controller
private $titleForUpdations = 'University';
//Definig a default variable for the folder name. Just renaming this variable will affect all the views
//making route with same folder name. changing name at this place enough for this controller
private $folderName = 'universities';
public function commonArray(){
//this array is common for all the function
$array = [
'mainTitle' => $this->titleForUpdations.' Management' ,
'secondTitle' => 'Add New '.$this->titleForUpdations ,
'add' => 'Add New '.$this->titleForUpdations ,
'back' => 'Back to '.$this->titleForUpdations.' list',
'detail' => $this->titleForUpdations.' Details',
'pluralName' => $this->titleForUpdations.'ies' ];
return $array;
}
然后我将此$array
传递给如下视图
public function index(University $model)
{
//fetching all data from model and paginating it when passing to view
return view('dboperations.'.$this->folderName.'.index', ['items' => $model->paginate(15) , $this-> commonArray()]);
}
然后我尝试访问视图中的变量,如下所示
@extends('layouts.app', ['title' => __($mainTitle)])
@section('content')
@include('users.partials.header', ['title' => __($secondTitle)])
但这会连续返回错误未定义变量
如果我dd($this->commonArray)
在一个函数中,它会输出我想要的,如下所示
array:6 [▼
"mainTitle" => "University Management"
"secondTitle" => "Add New University"
"add" => "Add New University"
"back" => "Back to University list"
"detail" => "University Details"
"pluralName" => "Universityies"
]
有人能告诉我我有什么遗漏吗?
谢谢您没有为您的commonArray添加键名
return view('dboperations.'.$this->folderName.'.index', ['items' => $model->paginate(15) , 'commonArray' => $this-> commonArray()]);