Issues (902)

framework/rest/Controller.php (4 issues)

1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\rest;
9
10
use Yii;
11
use yii\filters\auth\CompositeAuth;
12
use yii\filters\ContentNegotiator;
13
use yii\filters\RateLimiter;
14
use yii\filters\VerbFilter;
15
use yii\web\Response;
16
17
/**
18
 * Controller is the base class for RESTful API controller classes.
19
 *
20
 * Controller implements the following steps in a RESTful API request handling cycle:
21
 *
22
 * 1. Resolving response format (see [[ContentNegotiator]]);
23
 * 2. Validating request method (see [[verbs()]]).
24
 * 3. Authenticating user (see [[\yii\filters\auth\AuthInterface]]);
25
 * 4. Rate limiting (see [[RateLimiter]]);
26
 * 5. Formatting response data (see [[serializeData()]]).
27
 *
28
 * For more details and usage information on Controller, see the [guide article on rest controllers](guide:rest-controllers).
29
 *
30
 * @author Qiang Xue <[email protected]>
31
 * @since 2.0
32
 */
33
class Controller extends \yii\web\Controller
34
{
35
    /**
36
     * @var string|array the configuration for creating the serializer that formats the response data.
37
     */
38
    public $serializer = 'yii\rest\Serializer';
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public $enableCsrfValidation = false;
43
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function behaviors()
49
    {
50 1
        return [
51 1
            'contentNegotiator' => [
52 1
                'class' => ContentNegotiator::className(),
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

52
                'class' => /** @scrutinizer ignore-deprecated */ ContentNegotiator::className(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
53 1
                'formats' => [
54 1
                    'application/json' => Response::FORMAT_JSON,
55 1
                    'application/xml' => Response::FORMAT_XML,
56 1
                ],
57 1
            ],
58 1
            'verbFilter' => [
59 1
                'class' => VerbFilter::className(),
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

59
                'class' => /** @scrutinizer ignore-deprecated */ VerbFilter::className(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
60 1
                'actions' => $this->verbs(),
61 1
            ],
62 1
            'authenticator' => [
63 1
                'class' => CompositeAuth::className(),
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

63
                'class' => /** @scrutinizer ignore-deprecated */ CompositeAuth::className(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
64 1
            ],
65 1
            'rateLimiter' => [
66 1
                'class' => RateLimiter::className(),
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

66
                'class' => /** @scrutinizer ignore-deprecated */ RateLimiter::className(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
67 1
            ],
68 1
        ];
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 69
    public function afterAction($action, $result)
75
    {
76 69
        $result = parent::afterAction($action, $result);
77 69
        return $this->serializeData($result);
78
    }
79
80
    /**
81
     * Declares the allowed HTTP verbs.
82
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
83
     * @return array the allowed HTTP verbs.
84
     */
85
    protected function verbs()
86
    {
87
        return [];
88
    }
89
90
    /**
91
     * Serializes the specified data.
92
     * The default implementation will create a serializer based on the configuration given by [[serializer]].
93
     * It then uses the serializer to serialize the given data.
94
     * @param mixed $data the data to be serialized
95
     * @return mixed the serialized data.
96
     */
97 69
    protected function serializeData($data)
98
    {
99 69
        return Yii::createObject($this->serializer)->serialize($data);
100
    }
101
}
102