CakePHP 2.x: How to return 404 HTTP status code when missing view
Introduction
By default, CakePHP 2.x returns 500 status code when missing view. This is because of View#_getViewFileName()
throws MissingViewException
in such a case.
Ref: https://github.com/cakephp/cakephp/blob/2.x/lib/Cake/View/View.php#L1031
This post will show one examples of how to return 404 status code.
Codes
So, you will need do the following.
- Create new
MyView
class which is subclass ofView
class. It throwsNotFoundException
- Modify your controller to use
MyView
class instead ofView
class
1. Create new MyView class
View/MyView.php
2. Modify your controller to use MyView class
Controller/FoosController.php