render() публичный метод

Instantiates the correct view class, hands it its data, and uses it to render the view output.
public render ( string | null $view = null, string | null $layout = null ) : Response
$view string | null View to use for rendering
$layout string | null Layout to use
Результат Cake\Network\Response A response object containing the rendered view.
Пример #1
1
 /**
  * Generate the response using the controller object.
  *
  * @param string $template The template to render.
  * @return void
  */
 protected function _outputMessage($template)
 {
     try {
         $this->controller->render($template);
         $event = new Event('Controller.shutdown', $this->controller);
         $this->controller->afterFilter($event);
         $this->controller->response->send();
     } catch (MissingViewException $e) {
         $attributes = $e->getAttributes();
         if (isset($attributes['file']) && strpos($attributes['file'], 'error500') !== false) {
             $this->_outputMessageSafe('error500');
         } else {
             $this->_outputMessage('error500');
         }
     } catch (\Exception $e) {
         $this->_outputMessageSafe('error500');
     }
 }
 protected function _invoke(Controller $controller)
 {
     $result = $controller->startupProcess();
     if ($result instanceof Response) {
         return $result;
     }
     $response = $controller->invokeAction();
     if ($response !== null && !$response instanceof Response) {
         throw new LogicException('Controller actions can only Cake\\Network\\Response instances');
     }
     if (!$response && $controller->autoRender) {
         $response = $controller->render();
     } elseif (!$response) {
         $response = $controller->response;
     }
     $result = $controller->shutdownProcess();
     if ($result instanceof Response) {
         return $result;
     }
     return $response;
 }
 /**
  * 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
1
 /**
  * Invoke a controller's action and wrapping methods.
  *
  * @param \Cake\Controller\Controller $controller The controller to invoke.
  * @return \Cake\Network\Response The response
  * @throws \LogicException If the controller action returns a non-response value.
  */
 protected function _invoke(Controller $controller)
 {
     $this->dispatchEvent('Dispatcher.invokeController', ['controller' => $controller]);
     $result = $controller->startupProcess();
     if ($result instanceof Response) {
         return $result;
     }
     $response = $controller->invokeAction();
     if ($response !== null && !$response instanceof Response) {
         throw new LogicException('Controller actions can only return Cake\\Network\\Response or null.');
     }
     if (!$response && $controller->autoRender) {
         $response = $controller->render();
     } elseif (!$response) {
         $response = $controller->response;
     }
     $result = $controller->shutdownProcess();
     if ($result instanceof Response) {
         return $result;
     }
     return $response;
 }
Пример #5
0
 /**
  * Execute a Crud action
  *
  * @param string $controllerAction Override the controller action to execute as.
  * @param array $args List of arguments to pass to the CRUD action (Usually an ID to edit / delete).
  * @return \Cake\Network\Response
  * @throws Exception If an action is not mapped.
  */
 public function execute($controllerAction = null, $args = [])
 {
     $this->_loadListeners();
     $this->_action = $controllerAction ?: $this->_action;
     $action = $this->_action;
     if (empty($args)) {
         $args = $this->_request->params['pass'];
     }
     try {
         $event = $this->trigger('beforeHandle', $this->getSubject(compact('args', 'action')));
         $response = $this->action($event->subject->action)->handle($event->subject->args);
         if ($response instanceof Response) {
             return $response;
         }
     } catch (Exception $e) {
         if (isset($e->response)) {
             return $e->response;
         }
         throw $e;
     }
     $view = null;
     $crudAction = $this->action($action);
     if (method_exists($crudAction, 'view')) {
         $view = $crudAction->view();
     }
     return $this->_controller->response = $this->_controller->render($view);
 }
Пример #6
0
 /**
  * {@inheritDoc}
  */
 public function render($view = null, $layout = null)
 {
     if ($this->_isJsonActionRequest()) {
         return $this->renderJsonAction($view, $layout);
     }
     return parent::render($view, $layout);
 }
Пример #7
0
 /**
  * Generate the response using the controller object.
  *
  * @param string $template The template to render.
  * @return \Cake\Network\Response A response object that can be sent.
  */
 protected function _outputMessage($template)
 {
     try {
         $this->controller->render($template);
         return $this->_shutdown();
     } catch (MissingTemplateException $e) {
         $attributes = $e->getAttributes();
         if (isset($attributes['file']) && strpos($attributes['file'], 'error500') !== false) {
             return $this->_outputMessageSafe('error500');
         }
         return $this->_outputMessage('error500');
     } catch (MissingPluginException $e) {
         $attributes = $e->getAttributes();
         if (isset($attributes['plugin']) && $attributes['plugin'] === $this->controller->plugin) {
             $this->controller->plugin = null;
         }
         return $this->_outputMessageSafe('error500');
     } catch (Exception $e) {
         return $this->_outputMessageSafe('error500');
     }
 }
Пример #8
0
 /**
  * test that a component beforeRender can change the controller view class.
  *
  * @return void
  */
 public function testBeforeRenderEventCancelsRender()
 {
     $Controller = new Controller(new Request(), new Response());
     $Controller->eventManager()->attach(function ($event) {
         return false;
     }, 'Controller.beforeRender');
     $result = $Controller->render('index');
     $this->assertInstanceOf('Cake\\Network\\Response', $result);
 }
Пример #9
0
 /**
  * Route to a dynamic view if necessary
  * 
  * Override Controller::render()
  * 
  * If the developer requested this be a dynamic controller/action and this 
  * is one of the dynamic actions, render the proper view. Otherwise 
  * render according to normal rules.
  * 
  * @param type $view
  * @param type $layout
  * @return string
  */
 public function render($view = null, $layout = null)
 {
     if ($this->dynamic && in_array($this->request->action, $this->_crudActions)) {
         // This needs a place to put user created dynamic views?
         $view = is_null($view) ? "CrudViews.CRUD/{$this->request->action}" : $view;
         if (is_null($layout)) {
             $layout = !$this->layout ? 'default' : $this->layout;
         }
         // This still might need to detect custom action hookups
         // right now it only works for the standard views
         // but I think this is actually all we will want it to do
         // needs review and discussion
         return parent::render($view, $layout);
     } else {
         parent::render($view, $layout);
     }
     return $this->response;
 }
Пример #10
0
 public function render($view = false, $layout = false)
 {
     return parent::render($view, $layout);
 }
Пример #11
-1
 /**
  * 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}));
     }
 }
 /**
  * Renders the specified view with the model
  *
  * @param  string $view   The view to render
  * @param  Entity $model  The model to pass to the view
  * @param  string $layout The layout to use for the view
  * @return \Cake\Network\Response
  */
 public function renderModelView($view = null, $model = null, $layout = null)
 {
     $this->set(compact('model'));
     return parent::render($view, $layout);
 }
Пример #13
-4
 /**
  * Initializes the components and models a controller will be using.
  * Triggers the controller action and invokes the rendering if Controller::$autoRender
  * is true. If a response object is returned by controller action that is returned
  * else controller's $response property is returned.
  *
  * @param Controller $controller Controller to invoke
  * @return \Cake\Network\Response The resulting response object
  * @throws \Cake\Error\Exception If data returned by controller action is not an
  *   instance of Response
  */
 protected function _invoke(Controller $controller)
 {
     $controller->constructClasses();
     $result = $controller->startupProcess();
     if ($result instanceof Response) {
         return $result;
     }
     $response = $controller->invokeAction();
     if ($response !== null && !$response instanceof Response) {
         throw new Exception('Controller action can only return an instance of Response');
     }
     if (!$response && $controller->autoRender) {
         $response = $controller->render();
     } elseif (!$response) {
         $response = $controller->response;
     }
     $result = $controller->shutdownProcess();
     if ($result instanceof Response) {
         return $result;
     }
     return $response;
 }