本文整理汇总了PHP中Cake\Controller\Controller::viewBuilder方法的典型用法代码示例。如果您正苦于以下问题:PHP Controller::viewBuilder方法的具体用法?PHP Controller::viewBuilder怎么用?PHP Controller::viewBuilder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Controller\Controller
的用法示例。
在下文中一共展示了Controller::viewBuilder方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _getView
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();
}
示例2: _respondAsAjax
/**
* AjaxComponent::_respondAsAjax()
*
* @return void
*/
protected function _respondAsAjax()
{
$this->Controller->viewBuilder()->className($this->_config['viewClass']);
// Set flash messages to the view
if ($this->_config['flashKey']) {
$message = $this->Controller->request->session()->consume($this->_config['flashKey']);
$this->Controller->set('_message', $message);
}
// If _serialize is true, *all* viewVars will be serialized; no need to add _message.
if (!empty($this->Controller->viewVars['_serialize']) && $this->Controller->viewVars['_serialize'] === true) {
return;
}
$serializeKeys = ['_message'];
if (!empty($this->Controller->viewVars['_serialize'])) {
$serializeKeys = array_merge($serializeKeys, $this->Controller->viewVars['_serialize']);
}
$this->Controller->set('_serialize', $serializeKeys);
}
示例3: _unauthenticated
/**
* Handles unauthenticated access attempt. First the `unauthenticated()` method
* of the last authenticator in the chain will be called. The authenticator can
* handle sending response or redirection as appropriate and return `true` to
* indicate no further action is necessary. If authenticator returns null this
* method redirects user to login action. If it's an AJAX request and config
* `ajaxLogin` is specified that element is rendered else a 403 HTTP status code
* is returned.
*
* @param \Cake\Controller\Controller $controller A reference to the controller object.
* @return \Cake\Network\Response|null Null if current action is login action
* else response object returned by authenticate object or Controller::redirect().
*/
protected function _unauthenticated(Controller $controller)
{
if (empty($this->_authenticateObjects)) {
$this->constructAuthenticate();
}
$auth = end($this->_authenticateObjects);
$result = $auth->unauthenticated($this->request, $this->response);
if ($result !== null) {
return $result;
}
if (!$this->storage()->redirectUrl()) {
$this->storage()->redirectUrl($this->request->here(false));
}
if (!$controller->request->is('ajax')) {
$this->flash($this->_config['authError']);
$this->storage()->redirectUrl($controller->request->here(false));
return $controller->redirect($this->_config['loginAction']);
}
if (!empty($this->_config['ajaxLogin'])) {
$controller->viewBuilder()->templatePath('Element');
$response = $controller->render($this->_config['ajaxLogin'], $this->RequestHandler->ajaxLayout);
$response->statusCode(403);
return $response;
}
$this->response->statusCode(403);
return $this->response;
}
示例4: _createSitemap
protected function _createSitemap($type)
{
$this->_type = $type;
//$this->controller->autoRender = false;
$this->controller->viewClass = 'Sitemap.SitemapXml';
$this->controller->viewBuilder()->autoLayout(false);
//@TODO Set response parameters from SitemapXml view
$this->controller->response->type('application/xml');
$this->controller->response->cache(mktime(0, 0, 0, date('m'), date('d'), date('Y')), $this->cache);
}
示例5: startup
/**
* SetupComponent::startup()
*
* - Sets the layout based on session value 'Setup.layout'.
*
* @param \Cake\Event\Event $event
* @return void
*/
public function startup(Event $event)
{
$layout = $this->request->session()->read('Setup.layout');
if ($layout) {
$this->Controller->viewBuilder()->layout($layout);
}
}
示例6: _outputMessageSafe
/**
* A safer way to render error messages, replaces all helpers, with basics
* and doesn't call component methods.
*
* @param string $template The template to render.
* @return \Cake\Network\Response A response object that can be sent.
*/
protected function _outputMessageSafe($template)
{
$helpers = ['Form', 'Html'];
$this->controller->helpers = $helpers;
$builder = $this->controller->viewBuilder();
$builder->helpers($helpers, false)->layoutPath('')->templatePath('Error');
$view = $this->controller->createView();
$this->controller->response->body($view->render($template, 'error'));
$this->controller->response->type('html');
return $this->controller->response;
}
示例7: testRender
/**
* testRender method
*
* @return void
*/
public function testRender()
{
Plugin::load('TestPlugin');
$request = new Request('controller_posts/index');
$request->params['action'] = 'index';
$Controller = new Controller($request, new Response());
$Controller->viewBuilder()->templatePath('Posts');
$result = $Controller->render('index');
$this->assertRegExp('/posts index/', (string) $result);
$Controller->viewBuilder()->template('index');
$result = $Controller->render();
$this->assertRegExp('/posts index/', (string) $result);
$result = $Controller->render('/Element/test_element');
$this->assertRegExp('/this is the test element/', (string) $result);
}
示例8: renderAs
/**
* Sets either the view class if one exists or the layout and template path of the view.
* The names of these are derived from the $type input parameter.
*
* ### Usage:
*
* Render the response as an 'ajax' response.
*
* ```
* $this->RequestHandler->renderAs($this, 'ajax');
* ```
*
* Render the response as an xml file and force the result as a file download.
*
* ```
* $this->RequestHandler->renderAs($this, 'xml', ['attachment' => 'myfile.xml'];
* ```
*
* @param Controller $controller A reference to a controller object
* @param string $type Type of response to send (e.g: 'ajax')
* @param array $options Array of options to use
* @return void
* @see RequestHandlerComponent::respondAs()
*/
public function renderAs(Controller $controller, $type, array $options = [])
{
$defaults = ['charset' => 'UTF-8'];
$viewClassMap = $this->config('viewClassMap');
if (Configure::read('App.encoding') !== null) {
$defaults['charset'] = Configure::read('App.encoding');
}
$options += $defaults;
$builder = $controller->viewBuilder();
if (array_key_exists($type, $viewClassMap)) {
$view = $viewClassMap[$type];
} else {
$view = Inflector::classify($type);
}
$viewClass = null;
if ($builder->className() === null) {
$viewClass = App::className($view, 'View', 'View');
}
if ($viewClass) {
$controller->viewClass = $viewClass;
$builder->className($viewClass);
} else {
if (empty($this->_renderType)) {
$builder->templatePath($builder->templatePath() . DS . $type);
} else {
$builder->templatePath(preg_replace("/([\\/\\\\]{$this->_renderType})\$/", DS . $type, $builder->templatePath()));
}
$this->_renderType = $type;
$builder->layoutPath($type);
}
$response = $this->response;
if ($response->getMimeType($type)) {
$this->respondAs($type, $options);
}
$helper = ucfirst($type);
if (!in_array($helper, $controller->helpers) && empty($controller->helpers[$helper])) {
$helperClass = App::className($helper, 'View/Helper', 'Helper');
if ($helperClass !== false) {
$controller->helpers[] = $helper;
}
}
}
示例9: fieldToggle
/**
* 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}));
}
}
示例10: _getController
/**
* {@inheritDoc}
*/
protected function _getController()
{
if (!($request = Router::getRequest(true))) {
$request = new Request();
}
$response = new Response();
try {
$controller = new TestAppsErrorController($request, $response);
$controller->viewBuilder()->layout('banana');
} catch (\Exception $e) {
$controller = new Controller($request, $response);
$controller->viewBuilder()->templatePath('Error');
}
return $controller;
}