logo
down
shadow

Yii2 restful not working


Yii2 restful not working

By : Joe Pych
Date : November 22 2020, 03:03 PM
To fix this issue By using this link I tried to have a restful app, but it's not working and always returned this: , This is my config/web.php:
code :
<?php

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],

    'components' => [
        'urlManager'=>[
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
            ],
        ],
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'a',
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ],
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;
<?php

namespace app\controllers;

use yii\rest\ActiveController;

class UserController extends ActiveController{
    public $modelClass = 'app\models\User';
}


Share : facebook icon twitter icon
Yii2 Restful API - Example to Add a New Action

Yii2 Restful API - Example to Add a New Action


By : aod314
Date : March 29 2020, 07:55 AM
this will help I am not sure if you are asking for extra actions beside CRUD or just for CRUD, so I write in details for both cases.
Firstly, the framework includes \yii\rest\ActiveController that provides typical restful API operation and URL management.
code :
'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => ['resource'],
            'pluralize' => false,
            'extraPatterns' => [
                'POST {id}/your_preferred_url' => 'xxxxx', // 'xxxxx' refers to 'actionXxxxx'
            ],
        ],
    ],
],
how set pagination of restful api in yii2?

how set pagination of restful api in yii2?


By : Pankaj jain
Date : March 29 2020, 07:55 AM
wish helps you You can change per page value by modifying HTTP header X-Pagination-Per-Page
Check docs for more information here
Yii2 RESTFUL API without models: getting 404 response

Yii2 RESTFUL API without models: getting 404 response


By : ED LYONS
Date : November 01 2020, 04:09 PM
wish of those help The problem was with urlManager rules configuration. For e.g. if I create CarController it treats like CarsController. So I have to set pluralize to false. Also modified extraPatterns section as well:
code :
'urlManager'   => [
    'enablePrettyUrl'     => true,
    'enableStrictParsing' => true,
    'showScriptName'      => false,
    'rules'               => [
        [
            'class'         => 'yii\rest\UrlRule',
            'controller'    => [ 'v1/cars' ],
            'extraPatterns' => [ 'POST' => 'create' ],
        ],
        [
            'class'         => 'yii\rest\UrlRule',
            'controller'    => [ 'v1/car' ],
            'extraPatterns' => [ 'GET' => 'index', 'DELETE' => 'delete' ],
            'pluralize'     => false
        ],
        [
            'class'         => 'yii\rest\UrlRule',
            'controller'    => [ 'v1/notifications' ],
            'extraPatterns' => [ 'POST' => 'create' ]
        ]
    ]
]
Unable to get POST working with Yii2 RESTful API

Unable to get POST working with Yii2 RESTful API


By : Vitor Simoes
Date : March 29 2020, 07:55 AM
I hope this helps you . Turns out I was using the wrong end point. Using this end point worked for me:
http://localhost/test/create
Yii2 RESTful relational data

Yii2 RESTful relational data


By : Guerric Meurice
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I am having some trouble getting the Yii2 RESTful API returning relational data. I have this working when viewed through the frontend but i am trying to get the same data through the API and its not working the same way. , You need to remove all() part from your query. So the code should be:
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org