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;
    }
2
  • @Salem Ouerdani,yes. that was a typo. i am using the plural form. edited the question. Commented Oct 19, 2015 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 Commented Oct 20, 2015 at 8:09

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

8 Comments

I added the function as requested. It somehow is not accessible.
@DGT I just updated. this post may also help in case if what you need to implement is a filtering action with pagination support : stackoverflow.com/questions/25522462/yii2-rest-query
The thing is I am trying to implement something like 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?
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.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.