I want to rewrite JSON View in the RequestHandler. So there’s a file project_root/lib/JsonView.php
. What I want to do is to
- Import the
JsonView.php
file in another file inproject_root/app/View/CustomJsonView.php
. (I think I could useApp:import
, would it be right ?) - Choose this file as the custom in requestHandler like this:
public $components = array('RequestHandler' => array( 'viewClassMap' => array('json' => '/right/way/to/this/file/CustomJsonView', )));
But how do I write the right way for this file ?
I also saw this one https://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html#RequestHandlerComponent::viewClassMap
but there is no explanation about the right paths to the file. My CakePHP version is 2.4.4 .
Advertisement
Answer
You are not supposed to pass full paths, but “short classnames”, just like shown in the linked example, where ApiKit.MyJson
refers to the MyJsonView
view class in the ApiKit
plugin, which could be located in app/Plugin/ApiKit/View/MyJsonView.php
.
If you follow the conventions and place your CustomJsonView
class in app/View/CustomJsonView.php
as shown in the docs, you then just pass CustomJson
as the short classname in the request handlers viewClassMap
option.
Whether you use App::import()
or just require
to include the /lib/JsonView.php
file, is up to you, both works. In any way you must make sure that whatever you are importing there doesn’t clash with existing class names (JsonView
is a kinda reserved name as it already exists in the core), and that it is either following the CakePHP view class naming conventions, or you must extend it.
See also