Renamed Controller::view to Controller::viewClass and Controller::view, View::view can now be used to change the view rendered by default. Parameters of Controller::render() and View::render() changed to eliminate redundancy. Closes #1520

This commit is contained in:
ADmad 2011-02-13 02:24:39 +05:30
parent e16d21eaa3
commit 681b9997b0
3 changed files with 54 additions and 41 deletions

View file

@ -31,12 +31,12 @@ App::import('View', 'View', false);
* automatic model availability, redirection, callbacks, and more.
*
* Controllers should provide a number of 'action' methods. These are public methods on the controller
* that are not prefixed with a '_' and not part of Controller. Each action serves as an endpoint for
* that are not prefixed with a '_' and not part of Controller. Each action serves as an endpoint for
* performing a specific action on a resource or collection of resources. For example adding or editing a new
* object, or listing a set of objects.
*
* You can access request parameters, using `$this->request`. The request object contains all the POST, GET and FILES
* that were part of the request.
* that were part of the request.
*
* After performing the required actions, controllers are responsible for creating a response. This usually
* takes the form of a generated View, or possibly a redirection to another controller action. In either case
@ -86,7 +86,7 @@ class Controller extends Object {
/**
* An instance of a CakeRequest object that contains information about the current request.
* This object contains all the information about a request and several methods for reading
* additional information about the request.
* additional information about the request.
*
* @var CakeRequest
*/
@ -134,6 +134,15 @@ class Controller extends Object {
*/
public $modelNames = array();
/**
* The name of the view file to render. The name specified
* is the filename in /app/views/<sub_folder> without the .ctp extension.
*
* @var string
* @link http://book.cakephp.org/view/962/Page-related-Attributes-layout-and-pageTitle
*/
public $view = null;
/**
* The name of the layout file to render the view inside of. The name specified
* is the filename of the layout in /app/views/layouts without the .ctp
@ -182,7 +191,7 @@ class Controller extends Object {
*
* @var string
*/
public $view = 'View';
public $viewClass = 'View';
/**
* Instance of the View created during rendering. Won't be set until after Controller::render() is called.
@ -431,7 +440,7 @@ class Controller extends Object {
}
$plugin = $pluginName . '.';
}
if (is_subclass_of($this, $this->_mergeParent) || !empty($pluginController)) {
$appVars = get_class_vars($this->_mergeParent);
$uses = $appVars['uses'];
@ -447,7 +456,7 @@ class Controller extends Object {
array_unshift($this->uses, $plugin . $this->modelClass);
}
} elseif (
($this->uses !== null || $this->uses !== false) &&
($this->uses !== null || $this->uses !== false) &&
is_array($this->uses) && !empty($appVars['uses'])
) {
$this->uses = array_merge($this->uses, array_diff($appVars['uses'], $this->uses));
@ -459,7 +468,7 @@ class Controller extends Object {
$merge = array('components', 'helpers');
$appVars = get_class_vars($pluginController);
if (
($this->uses !== null || $this->uses !== false) &&
($this->uses !== null || $this->uses !== false) &&
is_array($this->uses) && !empty($appVars['uses'])
) {
$this->uses = array_merge($this->uses, array_diff($appVars['uses'], $this->uses));
@ -636,7 +645,7 @@ class Controller extends Object {
extract($status, EXTR_OVERWRITE);
}
$response = $this->Components->trigger(
'beforeRedirect',
'beforeRedirect',
array(&$this, $url, $status, $exit),
array('break' => true, 'breakOn' => false, 'collectReturn' => true)
);
@ -815,21 +824,20 @@ class Controller extends Object {
/**
* Instantiates the correct view class, hands it its data, and uses it to render the view output.
*
* @param string $action Action name to render
* @param string $view View to use for rendering
* @param string $layout Layout to use
* @param string $file File to use for rendering
* @return string Full output string of view contents
* @link http://book.cakephp.org/view/980/render
*/
public function render($action = null, $layout = null, $file = null) {
public function render($view = null, $layout = null) {
$this->beforeRender();
$this->Components->trigger('beforeRender', array(&$this));
$viewClass = $this->view;
if ($this->view != 'View') {
$viewClass = $this->viewClass;
if ($this->viewClass != 'View') {
list($plugin, $viewClass) = pluginSplit($viewClass);
$viewClass = $viewClass . 'View';
App::import('View', $this->view);
App::import('View', $this->viewClass);
}
$this->request->params['models'] = $this->modelNames;
@ -865,7 +873,7 @@ class Controller extends Object {
$this->autoRender = false;
$this->View = $View;
return $this->response->body($View->render($action, $layout, $file));
return $this->response->body($View->render($view, $layout));
}
/**

View file

@ -28,9 +28,9 @@ App::import('View', 'Helper', false);
* in from the controller to render the results of the controller action. Often this is HTML,
* but can also take the form of JSON, XML, PDF's or streaming files.
*
* CakePHP uses a two-step-view pattern. This means that the view content is rendered first,
* CakePHP uses a two-step-view pattern. This means that the view content is rendered first,
* and then inserted into the selected layout. A special `$content_for_layout` variable is available
* in the layout, and it contains the rendered view. This also means you can pass data from the view to the
* in the layout, and it contains the rendered view. This also means you can pass data from the view to the
* layout using `$this->set()`
*
* @package cake.libs.view
@ -87,6 +87,13 @@ class View extends Object {
*/
public $viewVars = array();
/**
* Name of view to use with this View.
*
* @var string
*/
public $view = null;
/**
* Name of layout to use with this View.
*
@ -123,7 +130,7 @@ class View extends Object {
* @var string
*/
public $subDir = null;
/**
* Theme name. If you are using themes, you should remember to use ThemeView as well.
*
@ -212,7 +219,7 @@ class View extends Object {
/**
* An instance of a CakeRequest object that contains information about the current request.
* This object contains all the information about a request and several methods for reading
* additional information about the request.
* additional information about the request.
*
* @var CakeRequest
*/
@ -234,7 +241,7 @@ class View extends Object {
* @var array
*/
private $__passedVars = array(
'viewVars', 'autoLayout', 'ext', 'helpers', 'layout', 'name',
'viewVars', 'autoLayout', 'ext', 'helpers', 'view', 'layout', 'name',
'layoutPath', 'viewPath', 'request', 'plugin', 'passedArgs', 'cacheAction'
);
@ -279,7 +286,7 @@ class View extends Object {
/**
* Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
*
* This realizes the concept of Elements, (or "partial layouts") and the $params array is used to send
* This realizes the concept of Elements, (or "partial layouts") and the $params array is used to send
* data to be used in the element. Elements can be cached improving performance by using the `cache` option.
*
* ### Special params
@ -353,8 +360,7 @@ class View extends Object {
}
/**
* Renders view for given action and layout. If $file is given, that is used
* for a view filename (e.g. customFunkyView.ctp).
* Renders view for given view file and layout.
*
* Render triggers helper callbacks, which are fired before and after the view are rendered,
* as well as before and after the layout. The helper callbacks are called
@ -366,14 +372,12 @@ class View extends Object {
*
* If View::$autoRender is false and no `$layout` is provided, the view will be returned bare.
*
* @param string $action Name of action to render for, this will be used as the filename to render, unless
* $file is give as well.
* @param string $view Name of view file to use
* @param string $layout Layout to use.
* @param string $file Custom filename for view. Providing this will render a specific file for the given action.
* @return string Rendered Element
* @throws CakeException if there is an error in the view.
*/
public function render($action = null, $layout = null, $file = null) {
public function render($view = null, $layout = null) {
if ($this->hasRendered) {
return true;
}
@ -382,11 +386,7 @@ class View extends Object {
}
$this->output = null;
if ($file != null) {
$action = $file;
}
if ($action !== false && $viewFileName = $this->_getViewFileName($action)) {
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
$this->Helpers->trigger('beforeRender', array($viewFileName));
$this->output = $this->_render($viewFileName);
$this->Helpers->trigger('afterRender', array($viewFileName));
@ -447,7 +447,7 @@ class View extends Object {
}
/**
* Render cached view. Works in concert with CacheHelper and Dispatcher to
* Render cached view. Works in concert with CacheHelper and Dispatcher to
* render cached view files.
*
* @param string $filename the cache file to include
@ -699,7 +699,7 @@ class View extends Object {
}
}
$paths = $this->_paths($this->plugin);
$exts = $this->_getExtensions();
foreach ($exts as $ext) {
foreach ($paths as $path) {

View file

@ -134,7 +134,7 @@ class ControllerCommentsController extends ControllerTestAppController {
* @access public
*/
public $name = 'ControllerComments';
protected $_mergeParent = 'ControllerTestAppController';
}
@ -277,7 +277,7 @@ class TestController extends ControllerTestAppController {
* @access public
*/
public $uses = array('ControllerComment', 'ControllerAlias');
protected $_mergeParent = 'ControllerTestAppController';
/**
@ -343,7 +343,7 @@ class TestComponent extends Object {
*/
function beforeRender($controller) {
if ($this->viewclass) {
$controller->view = $this->viewclass;
$controller->viewClass = $this->viewclass;
}
}
}
@ -368,7 +368,7 @@ class AnotherTestController extends ControllerTestAppController {
* @access public
*/
public $uses = null;
protected $_mergeParent = 'ControllerTestAppController';
}
@ -627,7 +627,7 @@ class ControllerTest extends CakeTestCase {
$Controller->set(array(1 => 'one', 2 => 'two'));
$expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
$this->assertEqual($Controller->viewVars, $expected);
}
/**
@ -641,7 +641,7 @@ class ControllerTest extends CakeTestCase {
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
), true);
$request = new CakeRequest('controller_posts/index');
$request->params['action'] = 'index';
$Controller = new Controller($request, $this->getMock('CakeResponse'));
$Controller->viewPath = 'posts';
@ -649,8 +649,13 @@ class ControllerTest extends CakeTestCase {
$result = $Controller->render('index');
$this->assertPattern('/posts index/', $result);
$Controller->view = 'index';
$result = $Controller->render();
$this->assertPattern('/posts index/', $result);
$result = $Controller->render('/elements/test_element');
$this->assertPattern('/this is the test element/', $result);
$Controller->view = null;
$Controller = new TestController($request);
$Controller->helpers = array('Html');
@ -986,7 +991,7 @@ class ControllerTest extends CakeTestCase {
$this->assertEqual($result, '/posts/index');
$request = $this->getMock('CakeRequest');
$request->expects($this->any())->method('referer')
->with(false)
->will($this->returnValue('http://localhost/posts/index'));