Diego Felipe
Diego Felipe

Reputation: 391

Yii2 Web Service not returning a single row from database

I've made a web service using yii2 basic template I got a table called 'ely_usuario' when I call it with:

http://localhost/basic/web/index.php/ely-usuario/

it works fine and returns me all the rows in ely_usuario table

but when I try to get just one record, for example:

http://localhost/basic/web/index.php/ely-usuario/29

it doesn't work, show me a not found page, I've made the model class using gii

here's my Controller:

<?php
namespace app\controllers;

use yii\rest\ActiveController;

class ElyUsuarioController extends ActiveController
{
    public $modelClass = 'app\models\ElyUsuario';
}

My configs:

'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => false,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => 'ely-usuario'],
            ],
        ],

Another weird thing that you might noticed is that 'enableStrictParsing' is false, in the yii2 guide it says to be true but for me it only works with false

Thanks

Upvotes: 0

Views: 97

Answers (1)

Ravi Singh
Ravi Singh

Reputation: 55

You need to change the code in your configs.I hope you will get an idea from the following code.It works fine for me!

'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:(ely-usuario)>/<action>/<id:\d+>' => '<controller>/<action>',
],
],

And in your controller please check your specific action.It must be coded like as:

public function actionTransactions($id=null){
if($id!=null){
//retrieve single row
}else{
 //retrieve multiple rows
}

Also please check this link for reference:

Why RESTfull API request to view return 404 in Yii2?

I hope it helps!

Upvotes: 0

Related Questions