Browse Source

Add accessor method for View::$viewPath

ADmad 10 years ago
parent
commit
18766a61b4

+ 1 - 1
src/Controller/Component/AuthComponent.php

@@ -349,7 +349,7 @@ class AuthComponent extends Component
         }
 
         if (!empty($this->_config['ajaxLogin'])) {
-            $controller->getview()->viewPath = 'Element';
+            $controller->getview()->viewPath('Element');
             $response = $controller->render(
                 $this->_config['ajaxLogin'],
                 $this->RequestHandler->ajaxLayout

+ 4 - 4
src/Controller/Component/RequestHandlerComponent.php

@@ -558,13 +558,13 @@ class RequestHandlerComponent extends Component
             $controller->viewClass = $viewClass;
         } else {
             if (empty($this->_renderType)) {
-                $controller->getView()->viewPath .= DS . $type;
+                $controller->getView()->viewPath($controller->getView()->viewPath() . DS . $type);
             } else {
-                $controller->getView()->viewPath = preg_replace(
+                $controller->getView()->viewPath(preg_replace(
                     "/([\/\\\\]{$this->_renderType})$/",
                     DS . $type,
-                    $controller->getView()->viewPath
-                );
+                    $controller->getView()->viewPath()
+                ));
             }
 
             $this->_renderType = $type;

+ 3 - 3
src/Controller/Controller.php

@@ -553,7 +553,7 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
     public function render($view = null, $layout = null)
     {
         $this->View = $this->getView();
-        if (!$this->_view->viewPath) {
+        if (!$this->_view->viewPath()) {
             $this->_viewPath();
         }
 
@@ -574,7 +574,7 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
 
         if ($viewClass !== $this->viewClass) {
             $this->View = $this->getView($this->viewClass);
-            if (!$this->_view->viewPath) {
+            if (!$this->_view->viewPath()) {
                 $this->_viewPath();
             }
         }
@@ -602,7 +602,7 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
             );
             $viewPath = implode(DS, $prefixes) . DS . $viewPath;
         }
-        $this->_view->viewPath = $viewPath;
+        $this->_view->viewPath($viewPath);
     }
 
     /**

+ 1 - 1
src/Controller/ErrorController.php

@@ -56,6 +56,6 @@ class ErrorController extends Controller
      */
     public function beforeRender(Event $event)
     {
-        $this->getView()->viewPath = 'Error';
+        $this->getView()->viewPath('Error');
     }
 }

+ 1 - 1
src/Error/ExceptionRenderer.php

@@ -332,7 +332,7 @@ class ExceptionRenderer
         $this->controller->helpers = ['Form', 'Html'];
         $view = $this->controller->createView();
         $view->layoutPath = null;
-        $view->viewPath = 'Error';
+        $view->viewPath('Error');
 
         $this->controller->response->body($view->render($template, 'error'));
         $this->controller->response->type('html');

+ 15 - 0
src/View/View.php

@@ -337,6 +337,21 @@ class View implements EventDispatcherInterface
     }
 
     /**
+     * Get/set path for view files.
+     *
+     * @param string $path Path for view files. If null returns current path.
+     * @return string|void
+     */
+    public function viewPath($path = null)
+    {
+        if ($path === null) {
+            return $this->viewPath;
+        }
+
+        $this->viewPath = $path;
+    }
+
+    /**
      * 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

+ 1 - 1
tests/test_app/TestApp/Controller/RequestHandlerTestController.php

@@ -32,7 +32,7 @@ class RequestHandlerTestController extends Controller
      */
     public function destination()
     {
-        $this->getView()->viewPath = 'Posts';
+        $this->getView()->viewPath('Posts');
         $this->render('index');
     }