I'll try to simplify the question a little more. In index.blade.php I have this AJAX call that gets routed to my controller. I added a "name" to the select element so that the form serialization is easier, and I've omitted the form. It POSTS with parameters:
command
_token
value1
value2
$( "#apiselection" ).submit(function( event ) {
alert( "Handler for .submit() called." );
event.preventDefault();
var formParams = $("#apiselection").serialize();
var data = formParams;
$.post("dicomgrid", data, function(result){
alert(result);
$("#APIresults").html(result);
});
});
In routes.php I have this:
Route::post('dicomgrid', 'DicomGridController@apiCall');
which is handled by the Contoller, which is now this. I had to add use Illuminate\Http\Request to make it work.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DicomGridController extends AppBaseController
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function apiCall(Request $request) {
if ($request->input('command') == "Login") {
return "test2";
}
else {
return "test";
}
}
}
That part works.
However, what I then want to do is essentially make an AJAX call (not sure you can do that within a Controller class using jQuery or some other method, so that what I return to the original call is what I get back through that second request. There must be a better way to do that, but I do like the idea of having a controller to service all of the requests for that particular application. I am really just getting started with this, so it is mostly a learning exercise.
To elaborate, the PHP page that has some of my functions is like this, cut down version:
<?php
$option = $_POST["APICall"];
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$APIBase = 'xxx';
$userName = "xxx";
$passWord = "xxx";
$sid =null;
$studyUUID;
function CallAPI($method, $API_command, $data = false)
{
global $curl; // curl object
global $APIBase; // base url for API calls
global $sid; // session id, null initially, but after login it is set for the session,
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_URL, $APIBase . $API_command );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if ($sid == null) {
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "login:password");
}
$result = curl_exec($curl);
return $result;
}
There are other functions on that same page that depend on the initialized variable and $curl in particular. Bottom line though is that I retrieve some results, either in JSON format or some other text (like formatted Array print_f), mostly for development and testing. That is why I want a controller to handle that feature set.
So I guess my question is how do I call my functions on that page within the Controller and return the results back to my original AJAX call.
It that a stupid question or method ?
Maybe this is what I need ?