A
A
Artur Kosikov2017-06-22 13:31:31
Yii
Artur Kosikov, 2017-06-22 13:31:31

If there is a rule in urlManager headers->set doesn't work?

I'm making a sitemap controller.
The idea is this: if you turn to / sitemap - sitemapindex is issued.
If you turn to /sitemap/type.xml - a urlset is issued.
For this, I registered in the config:

'urlManager' => [
           ...
            'rules' => [
                   '/sitemap/<file>' => 'sitemap/index', 
                   ....
            ]

Thus, in the sitemap controller, I get a $file that contains the desired type of posts to display in the urlset.
In the controller, I do:
public function actionIndex( $file = 0 )
    {
        // Заголовок Content-Type XML
        Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
        $headers = Yii::$app->response->headers;
        $headers->set('Content-Type', 'text/xml');
               if(!$file) {
                       // Здесь вывожу  sitemapindex;
                           return;
                       }
              // Здесь вывожу urlset для нужного $file
   }

Problem:
If you access /sitemap - everything is OK, the Content-Type: text/xml header is present
If you access /sitemap/type.xml - the Content-Type header is not sent (i.e. does not change, - it sends "text/html ")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-06-22
@webinar

I think that

$headers = Yii::$app->response->headers;
        $headers->set('Content-Type', 'text/xml');

doesn't work in the wrong place, it's not the url manager
, try this:
$headers = Yii::$app->response->headers;
$headers->add('Content-Type', 'text/xml');

A
Artur Kosikov, 2017-06-22
@atillus

Interesting... on /sitemap/xxxxxx (the URL is processed through urlmanager and sent to the sitemap controller) - it works simply: but if we directly access the /sitemap controller, then it only works like this:

$headers = Yii::$app->response->headers;
$headers->add('Content-Type', 'text/xml');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question