3

For some reason i am getting :

{
  "name": "Not Found",
  "message": "Object not found: search",
  "code": 0,
  "status": 404,
  "type": "yii\web\NotFoundHttpException"
}

when I try to access a custom action (http://localhost/project/api/web/v1/userfunctions/search) in my yii2 rest api app. This is what I have in the main.php configuration file

[
    'class' => 'yii\rest\UrlRule',
    'controller' => 'v1/userfunction',
    'extraPatterns' => ['GET search' => 'search'],
    'tokens' => [
        '{id}' => '<id:\\w+>'
    ]
]

And the UserFunctionController class has a actionSearch method.

Am I missing something here?

When I add a blank action method :

public function actions() 
{
  $actions = parent::actions();
  return $actions;
}

method the 404 goes away but i get a blank response (status code 200) [This is irrespective of whether the actionSearch is defined or not] Where does the control go in this case?

Here is the actionSearch() code

 public function actionSearch()
    { 
        $output = UserStatus::findAll();
        return $output;
    }
giovaZ
  • 1,432
  • 3
  • 20
  • 62
DGT
  • 391
  • 1
  • 3
  • 13
  • @Salem Ouerdani,yes. that was a typo. i am using the plural form. edited the question. – DGT Oct 19 '15 at 22:11
  • can you add the `actionSearch` function to your question ? all the code you did add should work just fine. maybe actionSearch is throwing an error or not returning an output – Salem Ouerdani Oct 20 '15 at 08:09

1 Answers1

0

By default Yii pluralize controller names for use in endpoints. So if you did not configure the yii\rest\UrlRule::$pluralize property to not do so, then your action should be available within :

http://localhost/project/api/web/v1/userfunctions/search

UPDATE:

public function actionSearch()
{ 
    $output = UserStatus::findAll();
    return $output;
}

This should throw this PHP error : Missing argument 1 for yii\db\BaseActiveRecord::findAll() as the findAll() method is expecting a mixed argument holding your filtering conditions.

You should use this instead :

public function actionSearch()
{ 
    $output = UserStatus::find()->all();
    return $output;
}

I think your server is not showing error messages correctly. Check its configs, errors log files and be sure to have those lines in your Entry Script while the app is in dev state :

// remember to remove them all when deploying to production

   error_reporting(E_ALL);
   ini_set('display_errors', 'on');

   defined('YII_DEBUG') or define('YII_DEBUG', true);
   defined('YII_ENV') or define('YII_ENV', 'dev');
Salem Ouerdani
  • 7,596
  • 3
  • 40
  • 52
  • I added the function as requested. It somehow is not accessible. – DGT Oct 21 '15 at 05:46
  • @DGT I just updated. this post may also help in case if what you need to implement is a filtering action with pagination support : http://stackoverflow.com/questions/25522462/yii2-rest-query – Salem Ouerdani Oct 21 '15 at 16:58
  • The thing is I am trying to implement something like http://api.test.loc/v1/model/xyz where I can call a sub link and send back custom data to the api response. But I keep getting the 404. Inside actionXyz() i will probably have something like Model::find()->select(['name', 'address']). The main concern is how to make the link work. Is 'extraPatterns' => ['GET xyz' => 'xyz'] in the main config and defining the actionXyz in the controller all? Or do i have to do anything else? – DGT Oct 26 '15 at 18:07
  • The $actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider']; method you mentioned in the other post works for me. But i want a link of the form api.test.loc/v1/products/xyz and not api.test.loc/v1/products?name=iphone. I appreciate your help. – DGT Oct 26 '15 at 18:13
  • Yes `extraPatterns` will do the work. in my answer in the other post, if you remove the `actions()` function and rename `indexDataProvider()` to `actionXyz()` then you add `'extraPatterns' => ['GET xyz' => 'xyz']` to your urlRules you'll have the same results within this url : `api.test.loc/v1/products/xyz?name=iphone` – Salem Ouerdani Oct 26 '15 at 19:51
  • But for some reason i get ```{ "name": "Not Found", "message": "Object not found: xyz", "code": 0, "status": 404, "type": "yii\web\NotFoundHttpException" } ``` This is the configuration: ```[ 'class' => 'yii\rest\UrlRule',` 'controller' => 'v1/model', 'extraPatterns' => ['GET xyz' => 'xyz'], 'tokens' => [ '{id}' => '' ] ], ``` – DGT Oct 26 '15 at 20:07
  • can you try without tokens ? are you receiving data when requesting `/v1/userfunctions` ? you are not overriding `verbs` inside you controller by declaring them again ? – Salem Ouerdani Oct 26 '15 at 20:13
  • taking the ```tokens``` out did the trick. It works perfectly now. And i do not override the ```verbs``` Thank you so much. – DGT Oct 26 '15 at 21:52