Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP allow searching by field using API?

I am trying to create an API using CakePHP that allows searching. For example:

http://localhost:8765/users/index/?username=admin

Which should return users with usernames equal to 'admin':

users: [
   {
      id: 3,
      username: "admin",
      image: "",
      firstName: "Jeremy",
      lastName: "Quick",
      userTypeId: 1,
      email: "[email protected]",
      groupId: 2
   }
]

So far, I have been able to accomplish this with a custom get() in the AppController which checks the $_GET and $_POST array for fields on the model. But the function is getting more and more complicated and verging on hackiness as I add more functionality (range search, collection search, and child table filtering). Is there a better, more CakePHP friendly way of accomplishing this? Whether through pure cakephp or a plugin?

like image 452
Jeremy Avatar asked Nov 08 '22 10:11

Jeremy


1 Answers

I think you want to use the Cakephp Search plugin. It has good documentation and uses a PRG method similar to what you are currently using. It will function just fine through an API. Here's a link to that plugin: github.com/FriendsOfCake/search

like image 159
AlbertMarkovski Avatar answered Nov 15 '22 07:11

AlbertMarkovski