Cakephp 2.9将变量从Controller传递到Layouts视图

时间:2017-09-06 07:04:18

标签: cakephp

我提出的问题非常相似,已经问过问题。但这对我有用。

ViewReportsController.php

class ViewReportsController extends AppController {
public function index() {
$count_table = 10;//sample variable that is available in view
$this->set('count_tablen',$count_table);
}
}

APP /视图/布局/ default.thtml中

 pr($count_tablen);

现在我收到的错误是 - 未定义变量:count_tablen [APP / View / Layouts / default.ctp,第228行]

1 个答案:

答案 0 :(得分:2)

您正在主布局模板中使用一个可能由多个控制器操作使用的变量。因此,您提供的代码示例仅适用于TIdFTP.Passive。如果要设置要在布局模板中使用的变量,则需要在select distinct (CRMAF_FilteredActivityPointer.activityid), CRMAF_FilteredActivityPointer.ownerid as ownerid, CRMAF_FilteredActivityPointer.owneridname as name, CRMAF_FilteredActivityPointer.activitytypecode, CRMAF_FilteredActivityPointer.activitytypecodename as activityType, CRMAF_FilteredActivityPointer.scheduledstart as start, CRMAF_FilteredActivityPointer.scheduledend as finish, CRMAF_FilteredActivityPointer.regardingobjectidname as client, CRMAF_FilteredActivityPointer.subject as subject, CRMAF_FilteredActivityPointer.description as description, dbo.GetKR_Sales_Chanel_Value(app.kr_sales_chanel,CRMAF_FilteredActivityPointer.activitytypecode) as kr_sales_chanel, app.kr_result as kr_result, dbo.kr_ActivityMembers(CRMAF_FilteredActivityPointer.activityid, CRMAF_FilteredActivityPointer.activitytypecode) as participants from FilteredActivityPointer as CRMAF_FilteredActivityPointer left outer join FilteredAppointment as app on CRMAF_FilteredActivityPointer.activityid=app.activityid left outer join FilteredEmail as em on CRMAF_FilteredActivityPointer.activityid=em.ActivityId where CRMAF_FilteredActivityPointer.activitytypecode=4212 or CRMAF_FilteredActivityPointer.activitytypecode=4201 or CRMAF_FilteredActivityPointer.activitytypecode=4202 or CRMAF_FilteredActivityPointer.activitytypecode=4210 order by kr_sales_chanel desc, CRMAF_FilteredActivityPointer.owneridname, CRMAF_FilteredActivityPointer.activitytypecodename asc, CRMAF_FilteredActivityPointer.scheduledend asc 的{​​{1}}回调中执行此操作,以便可以在任何地方使用它: -

/view_reports/index

如果使用多个布局模板,您可以在设置变量之前检查beforeRender中将使用的模板: -

AppController