Browse Source

Deriving the controller name out of the request object, if present

Jose Lorenzo Rodriguez 11 years ago
parent
commit
ef33e4e7da
2 changed files with 14 additions and 9 deletions
  1. 9 4
      src/Controller/Controller.php
  2. 5 5
      tests/TestCase/Controller/ControllerTest.php

+ 9 - 4
src/Controller/Controller.php

@@ -250,14 +250,19 @@ class Controller implements EventListenerInterface
      */
     public function __construct(Request $request = null, Response $response = null, $name = null, $eventManager = null)
     {
-        if ($this->name === null && $name === null) {
-            list(, $name) = namespaceSplit(get_class($this));
-            $name = substr($name, 0, -10);
-        }
         if ($name !== null) {
             $this->name = $name;
         }
 
+        if ($this->name === null && isset($request->params['controller'])) {
+            $this->name = $request->params['controller'];
+        }
+
+        if ($this->name === null) {
+            list(, $name) = namespaceSplit(get_class($this));
+            $this->name = substr($name, 0, -10);
+        }
+
         $this->setRequest($request !== null ? $request : new Request);
         $this->response = $response !== null ? $response : new Response;
 

+ 5 - 5
tests/TestCase/Controller/ControllerTest.php

@@ -762,7 +762,7 @@ class ControllerTest extends TestCase
     public function testInvokeActionMissingAction()
     {
         $url = new Request('test/missing');
-        $url->addParams(['controller' => 'test_controller', 'action' => 'missing']);
+        $url->addParams(['controller' => 'Test', 'action' => 'missing']);
         $response = $this->getMock('Cake\Network\Response');
 
         $Controller = new TestController($url, $response);
@@ -779,7 +779,7 @@ class ControllerTest extends TestCase
     public function testInvokeActionPrivate()
     {
         $url = new Request('test/private_m/');
-        $url->addParams(['controller' => 'test_controller', 'action' => 'private_m']);
+        $url->addParams(['controller' => 'Test', 'action' => 'private_m']);
         $response = $this->getMock('Cake\Network\Response');
 
         $Controller = new TestController($url, $response);
@@ -796,7 +796,7 @@ class ControllerTest extends TestCase
     public function testInvokeActionProtected()
     {
         $url = new Request('test/protected_m/');
-        $url->addParams(['controller' => 'test_controller', 'action' => 'protected_m']);
+        $url->addParams(['controller' => 'Test', 'action' => 'protected_m']);
         $response = $this->getMock('Cake\Network\Response');
 
         $Controller = new TestController($url, $response);
@@ -813,7 +813,7 @@ class ControllerTest extends TestCase
     public function testInvokeActionBaseMethods()
     {
         $url = new Request('test/redirect/');
-        $url->addParams(['controller' => 'test_controller', 'action' => 'redirect']);
+        $url->addParams(['controller' => 'Test', 'action' => 'redirect']);
         $response = $this->getMock('Cake\Network\Response');
 
         $Controller = new TestController($url, $response);
@@ -829,7 +829,7 @@ class ControllerTest extends TestCase
     {
         $url = new Request('test/returner/');
         $url->addParams([
-            'controller' => 'test_controller',
+            'controller' => 'Test',
             'action' => 'returner',
             'pass' => []
         ]);