PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 2, 2022

[FIXED] CakePHP 3.7 - Access view variable from controller

 February 02, 2022     cakephp, cakephp-3.0, variables, view     No comments   

Issue

Inside AppController->beforeRender() I would to check whether a view variable is already set and see its value.

How can you access view variables from a controller in CakePHP 3.7?

This used to be possible with $this->viewVars['variable_name'], but it has been deprecated.

I tried $this->viewBuilder()->getVars() but that returns an empty array, while there certainly are view variables.


Solution

Summarising an answer from the comments: this is not directly possible.
Accessing the variables through the viewBuilder() won't work because it only receives the viewvars as the view is being built.

A custom element encapsulating the variables to be accessed could be a solution.

I ended by setting some variables as default: in AppController->initialize() the view variable status is set as "OK", other controllers and methods can then simply override it.



Answered By - Roberto
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing