protected function _getView($viewVars = []) { $Request = new Request(); $Response = new Response(); $Controller = new Controller($Request, $Response); $builder = $Controller->viewBuilder(); $builder->className('JsonApi\\View\\JsonApiView'); if ($viewVars) { $Controller->set($viewVars); } return $Controller->createView(); }
/** * Test render with a View file specified. * * @return void */ public function testRenderWithView() { $Request = new Request(); $Response = new Response(); $Controller = new Controller($Request, $Response); $Controller->name = 'Posts'; $data = ['User' => ['username' => 'fake'], 'Item' => [['name' => 'item1'], ['name' => 'item2']]]; $Controller->set('user', $data); $Controller->viewClass = 'Json'; $View = $Controller->createView(); $View->viewPath = $Controller->name; $output = $View->render('index'); $expected = json_encode(['user' => 'fake', 'list' => ['item1', 'item2'], 'paging' => null]); $this->assertSame($expected, $output); $this->assertSame('application/json', $Response->type()); }
/** * testRenderWithView method * * @return void */ public function testRenderWithView() { $Request = new Request(); $Response = new Response(); $Controller = new Controller($Request, $Response); $Controller->name = 'Posts'; $Controller->viewPath = 'Posts'; $data = array(array('User' => array('username' => 'user1')), array('User' => array('username' => 'user2'))); $Controller->set('users', $data); $Controller->viewClass = 'Xml'; $View = $Controller->createView(); $output = $View->render('index'); $expected = array('users' => array('user' => array('user1', 'user2'))); $expected = Xml::build($expected)->asXML(); $this->assertSame($expected, $output); $this->assertSame('application/xml', $Response->type()); $this->assertInstanceOf('Cake\\View\\HelperRegistry', $View->helpers()); }
/** * beforeRender * * beforeRender callback for Component. * * @param \Cake\Event\Event $event Event. * @return void */ public function beforeRender($event) { $this->Controller->set('searchFilters', $this->_normalize($this->config('filters'))); }
/** * Renders the response for the exception. * * @return \Cake\Network\Response The response to be sent. */ public function render() { $exception = $this->error; $code = $this->_code($exception); $method = $this->_method($exception); $template = $this->_template($exception, $method, $code); $unwrapped = $this->_unwrap($exception); $isDebug = Configure::read('debug'); if (($isDebug || $exception instanceof HttpException) && method_exists($this, $method)) { return $this->_customMethod($method, $unwrapped); } $message = $this->_message($exception, $code); $url = $this->controller->request->here(); if (method_exists($exception, 'responseHeader')) { $this->controller->response->header($exception->responseHeader()); } $this->controller->response->statusCode($code); $viewVars = ['message' => $message, 'url' => h($url), 'error' => $unwrapped, 'code' => $code, '_serialize' => ['message', 'url', 'code']]; if ($isDebug) { $viewVars['trace'] = Debugger::formatTrace($unwrapped->getTrace(), ['format' => 'array', 'args' => false]); $viewVars['_serialize'][] = 'trace'; } $this->controller->set($viewVars); if ($unwrapped instanceof CakeException && $isDebug) { $this->controller->set($unwrapped->getAttributes()); } return $this->_outputMessage($template); }
/** * Prepares error messages for FormHelper. * * @param \Comment\Model\Entity\Comment $comment The invalidated comment entity * to extract error messages * @return void */ protected function _setErrors(Comment $comment) { $arrayContext = $this->config('arrayContext'); foreach ((array) $comment->errors() as $field => $msg) { $arrayContext['errors']['comment'][$field] = $msg; } $this->config('arrayContext', $arrayContext); $this->_controller->set('_commentFormContext', $this->config('arrayContext')); }
/** * Setup modules for layout. * * @return void */ private function __setForLayout() { $positions = $this->Theme->getThemePositions(); $theme = Inflector::underscore(Configure::read('Theme.' . Theme::CLIENT_FRONT_END)); foreach ($positions as $position) { $cacheKey = $theme . '_' . $position; $modules = $this->_table->find()->where(['position' => $position])->order(['ordering' => 'ASC'])->cache($cacheKey, 'positions')->toArray(); if (!empty($modules)) { $this->_modulesForLayout[$position] = $modules; } } $this->_controller->set('module_for_layout', $this->_modulesForLayout); }
/** * Renders the response for the exception. * * @return \Cake\Network\Response The response to be sent. */ public function render() { $exception = $this->error; $code = $this->_code($exception); $method = $this->_method($exception); $template = $this->_template($exception, $method, $code); $isDebug = Configure::read('debug'); if (($isDebug || $exception instanceof HttpException) && method_exists($this, $method)) { return $this->_customMethod($method, $exception); } $message = $this->_message($exception, $code); $url = $this->controller->request->here(); if (method_exists($exception, 'responseHeader')) { $this->controller->response->header($exception->responseHeader()); } $this->controller->response->statusCode($code); $this->controller->set(array('message' => $message, 'url' => h($url), 'error' => $exception, 'code' => $code, '_serialize' => array('message', 'url', 'code'))); if ($exception instanceof CakeException && $isDebug) { $this->controller->set($this->error->getAttributes()); } return $this->_outputMessage($template); }
/** * test that a component beforeRender can change the controller view class. * * @return void */ public function testBeforeRenderCallbackChangingViewClass() { $Controller = new Controller(new Request(), new Response()); $Controller->eventManager()->on('Controller.beforeRender', function ($event) { $controller = $event->subject(); $controller->viewClass = 'Json'; }); $Controller->set(['test' => 'value', '_serialize' => ['test']]); $debug = Configure::read('debug'); Configure::write('debug', false); $result = $Controller->render('index'); $this->assertEquals('{"test":"value"}', $result->body()); Configure::write('debug', $debug); }
/** * beforeRender * * beforeRender callback for Components * * @return void */ public function beforeRender() { $this->Controller->set('menu', self::$data); }
public function beforeRender() { $this->controller->set('type', $this->_type); $this->controller->set('locations', $this->locations); }
/** * Toggle ajax field. * * @param Table $table * @param $id * @param $value * @param string $field */ public function fieldToggle(Table $table, $id, $value, $field = 'status') { $this->_controller->serializeAjax = false; if (empty($id) || $value === null) { throw new Exception(__d('union', 'Invalid content')); } $this->_controller->viewBuilder()->layout('ajax')->templatePath('Common'); $record = $table->get($id); $record->{$field} = (int) (!$value); if ($entity = $table->save($record)) { $this->_controller->set('record', $entity); $this->_controller->render('Union/Core.toggle'); } else { throw new Exception(__d('union', 'Failed toggling field {0} to {1}', $field, $record->{$field})); } }
protected function _render($name, $view, $layout, $viewVars) { $request = new Request(); $response = new Response(); $controller = new Controller($request, $response); $controller->name = $name; $controller->layout = $layout; $controller->viewPath = $name; $controller->viewClass = 'Gourmet\\Liquid\\View\\View'; $controller->set($viewVars); return $controller->createView()->render($view); }
/** * Called before Controller::redirect(). Allows you to replace the URL that will * be redirected to with a new URL. * * @param \Cake\Event\Event $event Event * @param string|array $url Either the string or URL array that is being redirected to. * @param \Cake\Network\Response $response * @return void */ public function beforeRedirect(Event $event, $url, Response $response) { if (!$this->respondAsAjax || !$this->_config['resolveRedirect']) { return; } $url = Router::url($url, true); $status = $response->statusCode(); $response->statusCode(200); $this->Controller->autoRender = true; $this->Controller->set('_redirect', compact('url', 'status')); $serializeKeys = ['_redirect']; if (!empty($this->Controller->viewVars['_serialize'])) { $serializeKeys = array_merge($serializeKeys, $this->Controller->viewVars['_serialize']); } $this->Controller->set('_serialize', $serializeKeys); $event->stopPropagation(); }
/** * Method that passes csv defined Table fields to the View * * @return void */ protected function _setTableFields() { $result = []; $pathFinder = new ViewPathFinder(); $path = $pathFinder->find($this->request->controller, $this->request->action); $result = $this->_getFieldsFromCsv($path); list($plugin, $model) = pluginSplit($this->_tableInstance->registryAlias()); /* add plugin and model names to each of the fields */ $result = $this->_setFieldPluginAndModel($result, $model, $plugin); /* If action requires panels, arrange the fields into the panels */ if (in_array($this->request->action, $this->_panelActions)) { $result = $this->_arrangePanels($result); } $this->_controllerInstance->set('fields', $result); $this->_controllerInstance->set('_serialize', ['fields']); }