Hi. In cakePhp 3 I need to define a default value for a variable used in my template file with fetch function.
For specific pages I assign the value with command $this->assign(‘variable_name’, ‘value_1’).
But how I can define the default value for variable_name?
Lots of ways you could do this, depending on where the knowledge about the default value should go, and the general structure of your application. You might do this in a beforeFilter
callback, for example, to set the default value before the controller runs, and then any future value that might be set will overwrite it. You might have if/else
code in your controller action to set it to the default if a specific value isn’t available. Or you might use isset
in the template to insert the default if it hasn’t already been set. Or, depending on the version of PHP you’re using, you might be able to use $variable_name ?? 'default_value'
when you need to reference it.