Renders data (usually the result of a controller action) and generates a string
representation of it, based on the type of expected output.
public static render ( object $response, mixed $data = null, array $options = [] ) : object | ||
$response | object | A Response object into which the operation will be rendered. The content of the render operation will be assigned to the `$body` property of the object, the `'Content-Type'` header will be set accordingly, and it will be returned. |
$data | mixed | The data (usually an associative array) to be rendered in the response. |
$options | array | Any options specific to the response being rendered, such as type information, keys (i.e. controller and action) used to generate template paths, etc. |
return | object | Returns a modified `Response` object with headers and body defined. |
/** * This tests that setting custom paths and disabling layout * via `\lithium\net\http\Media::type()` is handled properly * by the default `\lithium\template\View` class and `File` * rendered adapter. */ public function testMediaTypeViewRender() { Media::type('view-integration-test', 'lithium/viewtest', array('view' => 'lithium\\template\\View', 'paths' => array('layout' => false, 'template' => array('{:library}/tests/mocks/template/view/adapters/{:template}.{:type}.php', '{:library}/tests/mocks/template/view/adapters/{:template}.html.php')))); // testing no layout with a custom type template $response = new Response(); $response->type('view-integration-test'); Media::render($response, array(), array('layout' => true, 'library' => 'lithium', 'template' => 'testTypeFile')); $this->assertEqual('This is a type test.', $response->body()); // testing the template falls back to the html template $response = new Response(); $response->type('view-integration-test'); Media::render($response, array(), array('layout' => true, 'library' => 'lithium', 'template' => 'testFile')); $this->assertEqual('This is a test.', $response->body()); // testing with a layout Media::type('view-integration-test', 'lithium/viewtest', array('view' => 'lithium\\template\\View', 'paths' => array('layout' => '{:library}/tests/mocks/template/view/adapters/testLayoutFile.html.php', 'template' => array('{:library}/tests/mocks/template/view/adapters/{:template}.{:type}.php', '{:library}/tests/mocks/template/view/adapters/{:template}.html.php')))); $response = new Response(); $response->type('view-integration-test'); Media::render($response, array(), array('layout' => true, 'library' => 'lithium', 'template' => 'testTypeFile')); $this->assertEqual("Layout top.\nThis is a type test.Layout bottom.", $response->body()); }
/** * Tests that the `Response` object can be directly modified from a templating class or encode * function. * * @return void */ public function testResponseModification() { Media::type('my', 'text/x-my', array('view' => 'lithium\\tests\\mocks\\net\\http\\Template')); $response = new Response(); Media::render($response, null, array('type' => 'my')); $this->assertEqual('Value', $response->headers('Custom')); }
<?php /** * Lithium: the most rad php framework * * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ use lithium\core\ErrorHandler; use lithium\action\Response; use lithium\net\http\Media; use lithium\analysis\Debugger; ErrorHandler::apply('lithium\\action\\Dispatcher::run', array(), function ($info, $params) { $stack = Debugger::trace(array('format' => 'array', 'trace' => $info['exception']->getTrace())); $exception_class = get_class($info['exception']); array_unshift($stack, array('functionRef' => '[exception]', 'file' => $info['exception']->getFile(), 'line' => $info['exception']->getLine())); $response = new Response(array('request' => $params['request'], 'status' => $info['exception']->getCode())); Media::render($response, compact('info', 'params', 'stack', 'exception_class'), array('controller' => 'errors', 'template' => 'development', 'layout' => 'error', 'request' => $params['request'])); return $response; });
<?php /** * Lithium: the most rad php framework * * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ use lithium\core\ErrorHandler; use lithium\action\Response; use lithium\net\http\Media; ErrorHandler::apply('lithium\\action\\Dispatcher::run', array(), function ($info, $params) { $response = new Response(array('request' => $params['request'], 'status' => $info['exception']->getCode())); Media::render($response, compact('info', 'params'), array('library' => true, 'controller' => '_errors', 'template' => 'development', 'layout' => 'error', 'request' => $params['request'])); return $response; });
* @license http://opensource.org/licenses/bsd-license.php The BSD License */ use lithium\core\ErrorHandler; use lithium\action\Response; use lithium\net\http\Media; use lithium\core\Environment; use lithium\core\Libraries; ErrorHandler::apply('lithium\\action\\Dispatcher::run', array(), function ($info, $params) { $response = new Response(array('request' => $params['request'], 'status' => $info['exception']->getCode())); // Production error templates should follow the design of the site. $error_layout = 'default'; $error_template = 'production'; $appCfg = Libraries::get(); $defaultApp = false; $defaultLibrary = null; foreach ($appCfg as $k => $library) { if ($library['default'] === true) { $defaultApp = $library; $defaultLibrary = $k; } } // Development error templates can look different. if (Environment::is('development')) { $error_layout = file_exists($defaultApp['path'] . '/views/layouts/error.html.php') ? 'error' : $error_layout; $error_template = 'development'; } // If the error templates don't exist use li3b_core's. $error_library = file_exists($defaultApp['path'] . '/views/layouts/' . $error_layout . '.html.php') && file_exists($defaultApp['path'] . '/views/_errors/' . $error_template . '.html.php') ? $defaultLibrary : 'li3b_core'; Media::render($response, compact('info', 'params'), array('library' => $error_library, 'controller' => '_errors', 'template' => $error_template, 'layout' => $error_layout, 'request' => $params['request'])); return $response; });
<?php /** * Lithium: the most rad php framework * * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ use lithium\core\Libraries; use lithium\core\ErrorHandler; use lithium\action\Response; use lithium\net\http\Media; use lithium\analysis\Logger; ErrorHandler::apply('lithium\\action\\Dispatcher::run', array(), function ($info, $params) { if (preg_match('/not found/i', $info['exception']->getMessage())) { $code = 404; } else { $code = $info['exception']->getCode() == 404 ? 404 : 500; } $response = new Response(array('request' => $params['request'], 'status' => $code)); Media::render($response, compact('info', 'params'), array('library' => true, 'controller' => '_errors', 'template' => $code == 404 ? 'fourohfour' : 'fiveohoh', 'layout' => 'default', 'request' => $params['request'])); return $response; }); Logger::config(['default' => ['adapter' => 'File', 'path' => dirname(Libraries::get(true, 'path')) . '/log', 'timestamp' => '[Y-m-d H:i:s]', 'file' => function ($data, $config) { return 'app.log'; }, 'priority' => ['debug', 'error', 'notice', 'warning']]]);
$code = Inspector::lines($frame['file'], $lines); if ($code) { foreach ($code as $num => &$content) { $numPad = str_pad($num, 3, ' '); $content = str_ireplace(array('<?php', '?>'), '', $content); $content = highlight_string("<?php {$numPad}{$content} ?>", true); $content = str_replace($replace, '', $content); } } $frame += compact('location', 'lines', 'code'); } $code = $exception->getCode(); $class = get_class($exception); $message = $exception->getMessage(); $data = compact('info', 'params', 'class', 'stack', 'code', 'exception'); Media::render($response, $data, array('controller' => '_errors', 'type' => 'html', 'template' => 'development', 'layout' => 'error', 'request' => $params['request'])); return $response; }); /** * If you use regular lithium templates you should use this handler as the view * itself contains all the formatting setup that is taken care of here for * twig templates */ /* ErrorHandler::apply('lithium\action\Dispatcher::run', array(), function($info, $params) { $response = new Response(array('request' => $params['request'])); Media::render($response, compact('info', 'params'), array( 'controller' => '_errors', 'template' => 'development', 'layout' => 'error',
public function export($id = null) { $id = !is_null($id) ? $id : $this->request->id; $model = $this->scaffold['model']; $singular = strtolower($this->scaffold['singular']); $plural = strtolower($this->scaffold['table']); if (is_null($id)) { $limit = 0; $conditions = $this->_options(); $result = $model::find('all', compact('limit', 'conditions')); $data = array($model => $result); $suffix = !empty($conditions) ? http_build_query($conditions, '', '-') : date('Y-m-d_H:i:s'); $name = sprintf('%s-%s.json', $plural, $suffix); } else { $result = $model::first($id); $data = array($model => array($id => $result->data())); $name = sprintf('%s-%s.json', $singular, $id); } $this->response->headers('Content-Disposition', sprintf('attachment; filename=%s', $name)); $this->_render['hasRendered'] = true; return Media::render($this->response, $data, array('type' => 'json')); }
public function testRenderWithOptionsMerging() { $request = new Request(); $request->params['controller'] = 'pages'; $response = new Response(); $response->type = 'html'; Media::render($response, null, compact('request') + array('layout' => false, 'template' => 'home')); $this->assertPattern('/Home/', $response->body()); }