Browse Source

Add App::uses to baked controllers.

Controllers should load their dependencies.
Bake should help with the best practice.
Fixes #1971
mark_story 14 years ago
parent
commit
3cb3424b4f

+ 6 - 2
lib/Cake/Console/Command/Task/ControllerTask.php

@@ -290,7 +290,8 @@ class ControllerTask extends BakeTask {
 		$displayField = $modelObj->displayField;
 		$primaryKey = $modelObj->primaryKey;
 
-		$this->Template->set(compact('plugin', 'admin', 'controllerPath', 'pluralName', 'singularName',
+		$this->Template->set(compact(
+			'plugin', 'admin', 'controllerPath', 'pluralName', 'singularName',
 			'singularHumanName', 'pluralHumanName', 'modelObj', 'wannaUseSession', 'currentModelName',
 			'displayField', 'primaryKey'
 		));
@@ -312,7 +313,10 @@ class ControllerTask extends BakeTask {
 
 		$isScaffold = ($actions === 'scaffold') ? true : false;
 
-		$this->Template->set('plugin', $this->plugin);
+		$this->Template->set(array(
+			'plugin' => $this->plugin,
+			'pluginPath' => empty($this->plugin) ? '' : $this->plugin . '.'
+		));
 		$this->Template->set(compact('controllerName', 'actions', 'helpers', 'components', 'isScaffold'));
 		$contents = $this->Template->generate('classes', 'controller');
 

+ 1 - 0
lib/Cake/Console/Templates/default/classes/controller.ctp

@@ -20,6 +20,7 @@
  */
 
 echo "<?php\n";
+echo "App::uses('{$plugin}AppController', '{$pluginPath}Controller');\n";
 ?>
 /**
  * <?php echo $controllerName; ?> Controller

+ 6 - 3
lib/Cake/Test/Case/Console/Command/Task/ControllerTaskTest.php

@@ -315,15 +315,18 @@ class ControllerTaskTest extends CakeTestCase {
 		$this->Task->expects($this->at(3))->method('createFile')->with(
 			$path,
 			new PHPUnit_Framework_Constraint_PCREMatch('/ArticlesController extends ControllerTestAppController/')
-		);
+		)->will($this->returnValue(true));
 
 		$this->Task->bake('Articles', '--actions--', array(), array(), array());
 
 		$this->Task->plugin = 'ControllerTest';
 		$path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'Controller' . DS . 'ArticlesController.php';
-		$this->Task->bake('Articles', '--actions--', array(), array(), array());
+		$result = $this->Task->bake('Articles', '--actions--', array(), array(), array());
+
+		$this->assertContains("App::uses('ControllerTestAppController', 'ControllerTest.Controller');", $result);
+		$this->assertEquals('ControllerTest', $this->Task->Template->templateVars['plugin']);
+		$this->assertEquals('ControllerTest.', $this->Task->Template->templateVars['pluginPath']);
 
-		$this->assertEqual($this->Task->Template->templateVars['plugin'], 'ControllerTest');
 		CakePlugin::unload();
 	}