S
S
Stanislav Khmelevsky2015-05-24 11:46:22
PostgreSQL
Stanislav Khmelevsky, 2015-05-24 11:46:22

Yii2 REST, why doesn't routing and aliases for fields() work?

Good day.
We have:

  • VPS server Debian 7.8
  • Apache 2.2 (Black-end) + Nginx 1.2 (Front static)
  • Yii2 (Base application template)
  • PostgreSQL 9+

Configuration:
.htaccess directories:
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>

.htaccess web folders:
AddDefaultCharset utf-8

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

urlManager settings :
'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',
            ],
],

The content of the CodeController controller :
<?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;
            },
        ];
    }
}

Two tables:
Code
id INT
id_user INT
id_model INT
code VARCHAR(12) -- 12 символьный хэш

Models
id INT
name INT
map INT
...

What you need:
Set up REST, which will give data in the form of XML from two tables related
via relation (hasOne) . by a link like :
sitename.io/code?hash=Jgme3GnpQeL
by hash, we get the model ID from the code table
And by this ID we pull out all the data from the models table
What's the problem:
For the first time I'm trying to set up REST, I do everything strictly according to the official documentation .
But as usual, nothing works the first time.
Link of the form /code - returns all data
Link of the form /code/1 - returns 404
Link of the form /code?fields=id - returns all IDs
Link like /code?fields=id,mformat - returns ID only, alias mformat for format field doesn't work
Link like /code?fields=id,format - returns ID and format
Link like /code?fields=id,format,name - returns ID, format and name (the name is exactly the same as in the database, without the formatting that is specified in the controller)
I tried to solve the problem myself, stubbornly googled, but could not figure out why the aliases did not work.
extraFields are also ignored. Help me please!
PS URLmanager configuration is not optimal, I agree. If there are suggestions for improving it, I will gladly listen to them.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Khmelevsky, 2015-05-24
@socode

Found what was the problem.
It turns out fields() needs to be overridden
in the MODEL
hqdefault.jpg

M
matperez, 2015-05-24
@matperez

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 question

Ask a Question

731 491 924 answers to any question