Shubham Guleria
Shubham Guleria

Reputation: 1

Yii2 Render Page from a function

I have created an rest calling framework in YII2. In this I have a class APIRequest From this class I want to render a page if I get an error from API.

My code:

public static function response($response,$serviceObject)
{
    if($serviceObject->responseCode == 420)
    {
        $errorMessage   =   $response->errorMessage;
        return \Yii::$app->getView()->renderFile('@app/views/merchants/error.php',['errorMessage'=>$errorMessage]);
    }
    else 
    {
        return $response;
    }
}

But this is not working.

Upvotes: 0

Views: 661

Answers (3)

waseem asgar
waseem asgar

Reputation: 742

use

return \Yii::$app->view->renderFile('@app/views/merchants/error.php',['errorMessage'=>$errorMessage]);

instead of

return \Yii::$app->getView()->renderFile('@app/views/merchants/error.php',['errorMessage'=>$errorMessage])

Upvotes: 0

Tibor Nagy
Tibor Nagy

Reputation: 1236

Generally the Yii REST API doesn't use any views. See

http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

Upvotes: 0

Tim
Tim

Reputation: 305

Not much information to go on. where is this method invoked? in the controller class?

perhapse this could help http://www.yiiframework.com/doc-2.0/guide-runtime-handling-errors.html

Upvotes: 1

Related Questions