简体   繁体   中英

How do you change globally the default content type in Catalyst?

I've tried with:

  • MyApp/lib/MyApp.pm
__PACKAGE__->config(
    ...,
    content_type => 'application/xhtml+xml'
);

and even with:

  • MyApp/lib/MyApp/View/HTML.pm
__PACKAGE__->config(
    ...,
    content_type => 'application/xhtml+xml',
);

I'd do

$c->response->headers->content_type('text/plain');

in YourApp/Controller/Root.pm sub auto .

This should run before any other controller (method) and should give a reasonable default value.

Inheritance of your controllers is the key. (Then you do not need actions upon every request for this purpose.)

Modifying some basic packages' config is not enough, since Catalyst has no propagation mechanism for these configs - they are statically associated with the class. (This differs from the built-in special actions like "auto" or "default" - which are propagated down the namespace tree, I guess).

Create a DefaultConfig.pm class (Note: the example is with Catalyst::Controller::REST - since this is, what I am currently using, but Catalyst::Controller should work likewise.):

package MyApp::Controller::SomeArbitraryController;
use Moose;

BEGIN { extends 'MyApp::Controller::DefaultConfig' }

# No config reqired

Your controllers inherit from DefaultConfig.pm:

 package MyApp::Controller::SomeArbitraryController; use Moose; BEGIN { extends 'MyApp::Controller::DefaultConfig' } # No config reqired

Note, that you should not define 'namespace' and 'path' configs in your DefaultConfig.pm and thus break Catalyst's automatic naming of your controllers' paths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM