1

Consider my controller name is Api_example and I have extended REST_Controller. Now my confusion is

public function user_get(){
//Some code..
}
public function user_post(){
    //Some code..
}

Now, I am not able to understand what is the 'user' in that method is. And if I access user_get() method like localhost/api_example/user/get or localhost/api_example/user/post just to display some array data in json format. Its not working. Please help me.

Mayank Vadiya
  • 1,373
  • 2
  • 18
  • 30
Preetham Rao
  • 69
  • 1
  • 2
  • 12

2 Answers2

1

you only can access to GET method through browser:

localhost/index.php/api_example/user

If you want access to POST method, you must send a post petition, you can read more about POST, GET, PUT and DELETE, here What is difference between HTTP methods GET, POST, PUT and DELETE

the prefix is the name of function, you can name as you want, the important is the sufix GET, POST, PUT or DELETE. index is the name to the default function, the url is [server]/index.php/[controller_name]/[function_name]

For example:

localhost/index.php/api_example/user

localhost is the servername.

index.php is the codeigniter url segment for access to controller folder.

api_example is the name of controller.

user is the name of function (function user_get(){ ... })

Community
  • 1
  • 1
Felipe Pincheira
  • 414
  • 5
  • 19
  • Thanks for your answer. But I would like to know what is that exactly the prefix word like 'user' in 'user_get()'. I have seen many examples but someone have used 'index_get()/index_post()'. – Preetham Rao Dec 21 '15 at 12:18
  • I understood till there you expalined, Now if I follow that procedure I am getting an 'unknown method' exceptionfor user_post(). Do I have to change anything in routes.PHP? Right now I have mentioned ['api_example/user/(:any)']=['api_example/user_post']. But I am not sure of it. – Preetham Rao Dec 22 '15 at 02:20
  • you can't access to POST method from browser, only GET methods, you can access to POST method through ajax or some chrome extention. The url is the same, only change the type, if type is POST, automatically access to function user_post(){...} , if you type is GET access to function user_get(){...} – Felipe Pincheira Dec 22 '15 at 12:55
  • Thanks a lot. U saved my day. I have used the firefox extension-https://addons.mozilla.org/en-US/firefox/addon/restclient/. – Preetham Rao Dec 23 '15 at 03:48
0

You can use following url [for GET request]:

YOUR_BASE_URL/api/example/users
n8coder
  • 671
  • 4
  • 12