Skip to content

Commit 1ce9fc5

Browse files
committed
Pages controller should render 404 on missing view file
1 parent d992d3a commit 1ce9fc5

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

app/Controller/PagesController.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class PagesController extends AppController {
5050
*
5151
* @param mixed What page to display
5252
* @return void
53+
* @throws NotFoundException When the view file could not be found
54+
* or MissingViewException in debug mode.
5355
*/
5456
public function display() {
5557
$path = func_get_args();
@@ -70,6 +72,14 @@ public function display() {
7072
$title_for_layout = Inflector::humanize($path[$count - 1]);
7173
}
7274
$this->set(compact('page', 'subpage', 'title_for_layout'));
73-
$this->render(implode('/', $path));
75+
76+
try {
77+
$this->render(implode('/', $path));
78+
} catch (MissingViewException $e) {
79+
if (Configure::read('debug')) {
80+
throw $e;
81+
}
82+
throw new NotFoundException();
83+
}
7484
}
7585
}

lib/Cake/Console/Templates/skel/Controller/PagesController.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class PagesController extends AppController {
4242
*
4343
* @param mixed What page to display
4444
* @return void
45+
* @throws NotFoundException When the view file could not be found
46+
* or MissingViewException in debug mode.
4547
*/
4648
public function display() {
4749
$path = func_get_args();
@@ -62,6 +64,14 @@ public function display() {
6264
$title_for_layout = Inflector::humanize($path[$count - 1]);
6365
}
6466
$this->set(compact('page', 'subpage', 'title_for_layout'));
65-
$this->render(implode('/', $path));
67+
68+
try {
69+
$this->render(implode('/', $path));
70+
} catch (MissingViewException $e) {
71+
if (Configure::read('debug')) {
72+
throw $e;
73+
}
74+
throw new NotFoundException();
75+
}
6676
}
6777
}

lib/Cake/Test/Case/Controller/PagesControllerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,30 @@ public function testDisplay() {
5151
$this->assertEquals('TestTheme', $Pages->viewVars['page']);
5252
$this->assertEquals('Posts', $Pages->viewVars['subpage']);
5353
}
54+
55+
/**
56+
* Test that missing view renders 404 page in production
57+
*
58+
* @expectedException NotFoundException
59+
* @expectedExceptionCode 404
60+
* @return void
61+
*/
62+
public function testMissingView() {
63+
Configure::write('debug', 0);
64+
$Pages = new PagesController(new CakeRequest(null, false), new CakeResponse());
65+
$Pages->display('non_existing_page');
66+
}
67+
68+
/**
69+
* Test that missing view in debug mode renders missing_view error page
70+
*
71+
* @expectedException MissingViewException
72+
* @expectedExceptionCode 500
73+
* @return void
74+
*/
75+
public function testMissingViewInDebug() {
76+
Configure::write('debug', 1);
77+
$Pages = new PagesController(new CakeRequest(null, false), new CakeResponse());
78+
$Pages->display('non_existing_page');
79+
}
5480
}

lib/Cake/Test/test_app/Controller/PagesController.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class PagesController extends AppController {
5151
*
5252
* @param mixed What page to display
5353
* @return void
54+
* @throws NotFoundException When the view file could not be found
55+
* or MissingViewException in debug mode.
5456
*/
5557
public function display() {
5658
$path = func_get_args();
@@ -75,7 +77,15 @@ public function display() {
7577
'subpage' => $subpage,
7678
'title_for_layout' => $titleForLayout
7779
));
78-
$this->render(implode('/', $path));
80+
81+
try {
82+
$this->render(implode('/', $path));
83+
} catch (MissingViewException $e) {
84+
if (Configure::read('debug')) {
85+
throw $e;
86+
}
87+
throw new NotFoundException();
88+
}
7989
}
8090

8191
}

0 commit comments

Comments
 (0)