Skip to content

Commit 4810518

Browse files
committed
Fix issue where abstract or interface controllers
would be constructed by Dispatcher. Tests added. Fixes #2048
1 parent 115c656 commit 4810518

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/Cake/Routing/Dispatcher.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ protected function _getController($request, $response) {
159159
if (!$ctrlClass) {
160160
return false;
161161
}
162-
return new $ctrlClass($request, $response);
162+
$reflection = new ReflectionClass($ctrlClass);
163+
if ($reflection->isAbstract() || $reflection->isInterface()) {
164+
return false;
165+
}
166+
return $reflection->newInstance($request, $response);
163167
}
164168

165169
/**

lib/Cake/Test/Case/Routing/DispatcherTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ protected function _invoke(Controller $controller, CakeRequest $request, CakeRes
6464
class MyPluginAppController extends AppController {
6565
}
6666

67+
abstract class DispatcherTestAbstractController extends Controller {
68+
abstract public function index();
69+
}
70+
71+
interface DispatcherTestInterfaceController {
72+
public function index();
73+
}
74+
6775
/**
6876
* MyPluginController class
6977
*
@@ -680,6 +688,41 @@ public function testMissingController() {
680688
$controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
681689
}
682690

691+
/**
692+
* testMissingControllerInterface method
693+
*
694+
* @expectedException MissingControllerException
695+
* @expectedExceptionMessage Controller class DispatcherTestInterfaceController could not be found.
696+
* @return void
697+
*/
698+
public function testMissingControllerInterface() {
699+
Router::connect('/:controller/:action/*');
700+
701+
$Dispatcher = new TestDispatcher();
702+
Configure::write('App.baseUrl', '/index.php');
703+
$url = new CakeRequest('dispatcher_test_interface/index');
704+
$response = $this->getMock('CakeResponse');
705+
706+
$controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
707+
}
708+
709+
/**
710+
* testMissingControllerInterface method
711+
*
712+
* @expectedException MissingControllerException
713+
* @expectedExceptionMessage Controller class DispatcherTestAbstractController could not be found.
714+
* @return void
715+
*/
716+
public function testMissingControllerAbstract() {
717+
Router::connect('/:controller/:action/*');
718+
719+
$Dispatcher = new TestDispatcher();
720+
Configure::write('App.baseUrl', '/index.php');
721+
$url = new CakeRequest('dispatcher_test_abstract/index');
722+
$response = $this->getMock('CakeResponse');
723+
724+
$controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
725+
}
683726
/**
684727
* testDispatch method
685728
*

0 commit comments

Comments
 (0)