Yii Framework Forum: Best way to return html - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Best way to return html

#1 User is offline   skworden 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 516
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 24 June 2017 - 03:01 PM

The restful service by default returns xml, json and html. When setting the format param in the url to return xml and json they work fine however, when setting it to html it throws an error similar to ...response can't be an array.

I have manged to get it to work by doing something like the following in my Rest controller

public function actions() {
    $formatParam = $this->behaviors()['contentNegotiator']['formatParam'];
    if (\Yii::$app->request->get($formatParam) == 'html') {
        //don't return default actions use defined ones below
   	return;
    }
    return parent::actions();
}

public function actionIndex() {
    return $this->render('index');
}



It would be nice if i could just set html and it would call whatever action instead of the default one. My question is what is the best way to support html while still keeping the other formats listed above?
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 26 June 2017 - 01:06 PM

What is the output in case of HTML?
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#3 User is offline   skworden 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 516
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 27 June 2017 - 10:29 PM

The API returns a styled page that is rendered on a different domain. I have it working but the docs say it supports HTML by default and it appears it does not.
0

#4 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 29 June 2017 - 04:21 AM

Where in the docs it's pointed out that it supports HTML? That's definitely a mis-guide to fix.


It does not since in order to render HTML framework needs a view template.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#5 User is offline   skworden 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 516
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 29 June 2017 - 04:53 PM

I'll try and find it again as it was a couple of days ago or I could have just got it confused. Is what I'm doing the best way to handle HTML output via the restful service?
0

#6 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 30 June 2017 - 04:51 AM

Yes. What you're doing seems to be OK.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users