CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • AjaxView
  • Cell
  • Helper
  • HelperRegistry
  • JsonView
  • SerializedView
  • StringTemplate
  • View
  • ViewBlock
  • ViewBuilder
  • XmlView

Traits

  • CellTrait
  • StringTemplateTrait
  • ViewVarsTrait
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * Redistributions of files must retain the above copyright notice.
  8:  *
  9:  * @copyright     Copyright (c), Cake Software Foundation, Inc. (https://cakefoundation.org)
 10:  * @link          https://cakephp.org CakePHP(tm) Project
 11:  * @since         3.0.0
 12:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 13:  */
 14: namespace Cake\View;
 15: 
 16: use Cake\Event\EventDispatcherInterface;
 17: 
 18: /**
 19:  * Provides the set() method for collecting template context.
 20:  *
 21:  * Once collected context data can be passed to another object.
 22:  * This is done in Controller, TemplateTask and View for example.
 23:  *
 24:  * @property array $_validViewOptions
 25:  */
 26: trait ViewVarsTrait
 27: {
 28:     /**
 29:      * The name of default View class.
 30:      *
 31:      * @var string|null
 32:      * @deprecated 3.1.0 Use `$this->viewBuilder()->getClassName()`/`$this->viewBuilder()->setClassName()` instead.
 33:      */
 34:     public $viewClass;
 35: 
 36:     /**
 37:      * Variables for the view.
 38:      *
 39:      * Deprecated: This property will be removed in 4.x.
 40:      * Inside controller context use `$this->set()` instead, also see `$this->viewBuilder()->getVar()`.
 41:      * In view context it will be a protected property `View::$viewVars`.
 42:      *
 43:      * @var array
 44:      */
 45:     public $viewVars = [];
 46: 
 47:     /**
 48:      * The view builder instance being used.
 49:      *
 50:      * @var \Cake\View\ViewBuilder
 51:      */
 52:     protected $_viewBuilder;
 53: 
 54:     /**
 55:      * Get the view builder being used.
 56:      *
 57:      * @return \Cake\View\ViewBuilder
 58:      */
 59:     public function viewBuilder()
 60:     {
 61:         if (!isset($this->_viewBuilder)) {
 62:             $this->_viewBuilder = new ViewBuilder();
 63:         }
 64: 
 65:         return $this->_viewBuilder;
 66:     }
 67: 
 68:     /**
 69:      * Constructs the view class instance based on the current configuration.
 70:      *
 71:      * @param string|null $viewClass Optional namespaced class name of the View class to instantiate.
 72:      * @return \Cake\View\View
 73:      * @throws \Cake\View\Exception\MissingViewException If view class was not found.
 74:      */
 75:     public function createView($viewClass = null)
 76:     {
 77:         $builder = $this->viewBuilder();
 78:         if ($viewClass === null && $builder->getClassName() === null) {
 79:             $builder->setClassName($this->viewClass);
 80:             $this->viewClass = null;
 81:         }
 82:         if ($viewClass) {
 83:             $builder->setClassName($viewClass);
 84:         }
 85: 
 86:         $validViewOptions = isset($this->_validViewOptions) ? $this->_validViewOptions : [];
 87:         $viewOptions = [];
 88:         foreach ($validViewOptions as $option) {
 89:             if (property_exists($this, $option)) {
 90:                 $viewOptions[$option] = $this->{$option};
 91:             }
 92:         }
 93: 
 94:         $deprecatedOptions = [
 95:             'layout' => 'setLayout',
 96:             'view' => 'setTemplate',
 97:             'theme' => 'setTheme',
 98:             'autoLayout' => 'enableAutoLayout',
 99:             'viewPath' => 'setTemplatePath',
100:             'layoutPath' => 'setLayoutPath',
101:         ];
102:         foreach ($deprecatedOptions as $old => $new) {
103:             if (property_exists($this, $old)) {
104:                 $builder->{$new}($this->{$old});
105:                 $message = sprintf(
106:                     'Property $%s is deprecated. Use $this->viewBuilder()->%s() instead in beforeRender().',
107:                     $old,
108:                     $new
109:                 );
110:                 deprecationWarning($message);
111:             }
112:         }
113: 
114:         foreach (['name', 'helpers', 'plugin'] as $prop) {
115:             if (isset($this->{$prop})) {
116:                 $method = 'set' . ucfirst($prop);
117:                 $builder->{$method}($this->{$prop});
118:             }
119:         }
120:         $builder->setOptions($viewOptions);
121: 
122:         return $builder->build(
123:             $this->viewVars,
124:             isset($this->request) ? $this->request : null,
125:             isset($this->response) ? $this->response : null,
126:             $this instanceof EventDispatcherInterface ? $this->getEventManager() : null
127:         );
128:     }
129: 
130:     /**
131:      * Saves a variable or an associative array of variables for use inside a template.
132:      *
133:      * @param string|array $name A string or an array of data.
134:      * @param mixed $value Value in case $name is a string (which then works as the key).
135:      *   Unused if $name is an associative array, otherwise serves as the values to $name's keys.
136:      * @return $this
137:      */
138:     public function set($name, $value = null)
139:     {
140:         if (is_array($name)) {
141:             if (is_array($value)) {
142:                 $data = array_combine($name, $value);
143:             } else {
144:                 $data = $name;
145:             }
146:         } else {
147:             $data = [$name => $value];
148:         }
149:         $this->viewVars = $data + $this->viewVars;
150: 
151:         return $this;
152:     }
153: 
154:     /**
155:      * Get/Set valid view options in the object's _validViewOptions property. The property is
156:      * created as an empty array if it is not set. If called without any parameters it will
157:      * return the current list of valid view options. See `createView()`.
158:      *
159:      * @param string|array|null $options string or array of string to be appended to _validViewOptions.
160:      * @param bool $merge Whether to merge with or override existing valid View options.
161:      *   Defaults to `true`.
162:      * @return array The updated view options as an array.
163:      * @deprecated 3.7.0 Use ViewBuilder::setOptions() or any one of it's setter methods instead.
164:      */
165:     public function viewOptions($options = null, $merge = true)
166:     {
167:         deprecationWarning(
168:             'ViewVarsTrait::viewOptions() is deprecated, used ViewBuilder::setOptions() instead.'
169:         );
170: 
171:         if (!isset($this->_validViewOptions)) {
172:             $this->_validViewOptions = [];
173:         }
174: 
175:         if ($options === null) {
176:             return $this->_validViewOptions;
177:         }
178: 
179:         if (!$merge) {
180:             return $this->_validViewOptions = (array)$options;
181:         }
182: 
183:         return $this->_validViewOptions = array_merge($this->_validViewOptions, (array)$options);
184:     }
185: }
186: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs