0

I have set up Rest API's in codeigniter which are to be consumed by android app and also the Web app from same codeigniter setup. Below is my folder structure.

Controllers
    api
      -signupApi
  Signup

The signupApi is the actual rest API to be consumed by both android and web app. Signup is my actual signup screen controller where I have a form to be posted with user registration data to the signupApi.

if($_POST){
    // call the signupApi here and post the user registration data
}

If there is a signup POST request how do I call the signup API function which is there in the REST API controller and handle the request in the Signup controller. I did research about how to call a function from another controller function but could not get the right solution for me. Can anybody please suggest me how to proceed with it?

2
  • 1
    Does this help? stackoverflow.com/questions/49448677/… Commented Apr 10, 2018 at 17:56
  • not really.. I want a way to call the API endpoint present in the API controller with the data it takes. I also don't want to mix REST API with PHP controller logic so the API can be consumed by the android app blindly and the code also remains clean. Commented Apr 10, 2018 at 18:37

2 Answers 2

1

how do I call the signup API function which is there in the REST API controller and handle the request in the Signup controller

Sounds like you want to call a method in one controller from another controller. Most MVC frameworks (including CodeIgniter) intend a controller to only handle the request itself.

If you have logic that you need both controllers to implement, you'll want (and need) to put that logic into a model. CodeIgniter does initially give the impression that models are only for Database ORM interactions, but they are also the appropriate option for most shared logic you'll deal with.

If both methods are taking the exact same request in the same structure, merge the methods and handle the API vs Web App output based on the contents of those requests, or pass an additional parameter to reflect out the data should be output.

Sign up to request clarification or add additional context in comments.

Comments

0

cURL is the most flexible way to interact with a REST API as it was designed for exactly this sort of thing. You can set HTTP headers, HTTP parameters and plenty more. cURL to make a POST request

Here is an example:

function function_name()
{
    $username = 'admin';
    $password = '1234';

    // Set up and execute the curl process
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, 'http://localhost/site/index.php/example_api');
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_POST, 1);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array(
        'name' => 'name',
        'email' => '[email protected]'
    ));

    // Optional, delete this line if your API is open
    curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ':' . $password);

    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);

    $result = json_decode($buffer);

    if(isset($result->status) && $result->status == 'success')
    {
        echo 'Record inserted successfully...';
    }

    else
    {
        echo 'Something has gone wrong';
    }
}

Comments

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.