2

I am having trouble setting a mime-type in the sitemapxml.xml. I've found this, but it has no effect:

$this->RequestHandler->respondAs('xml');
$this->response->header(['Content-type' => 'application/xml']);

I also tried to add header('Content-type:text/xml') at the beginning of my sitemap.ctp view.

Community
  • 1
  • 1
Cobalt
  • 23
  • 3
  • Please always add proper context, in this case, mention where exactly your code snippet is located in your controller! – ndm Feb 14 '16 at 15:06

2 Answers2

0

Check the migration guide:

RequestHandlerComponent now switches the layout and template based on the parsed extension or Accept header in the beforeRender() callback instead of startup().

Cookbook > Appendices > 3.1 Migration Guide > RequestHandlerComponent

So this means that what you set there (wherever that is, but for sure it's not in the Controller::beforeRender() callback), will be overwritten after the controller action ran, and before the view is being rendered.

There are various ways to handle this.

  1. Make proper use of the request handler component, that is, enable extension parsing and supply an extension in your URL, or send a proper Accept header. That way the component will set the proper response type.

    This is the recommended way!

    See also Cookbook > Views > JSON and XML views

  2. Set the RequestHandlerComponent::$ext property, which is being evaluated before rendering, and will cause the request handler component to set the response type accordingly,

    $this->RequestHandler->ext = 'xml';
    
  3. Use RequestHandlerComponent::renderAs() to instruct the request handler component to use the configured XML view, which will override the "wrong" type set in the beforeRender() callback.

    $this->RequestHandler->renderAs($this, 'xml');
    
  4. Do not use the request handler component and set the response type directly on the response object.

    $this->response->type('xml');
    
ndm
  • 59,784
  • 9
  • 71
  • 110
0

Intersting, I've saw that $this->response->type works only if $this->render('view'); is set directly

Cobalt
  • 23
  • 3