Answer the question
In order to leave comments, you need to log in
Yii2 REST, why doesn't routing and aliases for fields() work?
Good day.
We have:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteBase /
RewriteRule ^(.*) web/index.php/$1 [QSA,L,NC]
#Блокирует доступ к бэкапам и различным файлам
<FilesMatch "(\.(bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$">
Order allow,deny
Deny from all
Satisfy All
</FilesMatch>
#Усиливаем защиту кук
<IfModule php5_module>
php_value session.cookie_httponly true
</IfModule>
#Блокирует просмотр директорий
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>
#Блокирует доступ к скрытым директориям
<IfModule mod_rewrite.c>
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)." - [F]
</IfModule>
AddDefaultCharset utf-8
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'code',
],
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'index' => 'site/index',
'' => 'site/index',
'auth' => 'site/auth',
'login' => 'site/login',
'registration' => 'site/registration',
'OAuthCallback' => 'site/OAuthCallback',
'profile' => 'profile/index',
],
],
<?php
namespace app\controllers;
use yii\rest\ActiveController;
class CodeController extends ActiveController
{
public $modelClass = 'app\models\Models';
public function fields()
{
return [
'id',
'mformat' => 'format',
'name' => function ($model) {
return $model->format . ' ' . $model->id;
},
];
}
}
id INT
id_user INT
id_model INT
code VARCHAR(12) -- 12 символьный хэш
id INT
name INT
map INT
...
Answer the question
In order to leave comments, you need to log in
Found what was the problem.
It turns out fields() needs to be overridden
in the MODEL
It seems that you simply have not redefined fields. Try debugging controller actions, see what the model returns.
If anything, extraFields in the request are specified through the expand parameter, perhaps you tried to get these parameters in the same way, through fields.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question