I’m a complete layman in laravel , and now I’m laravel for what

I had the function 1.php :

  require_once('include/config.php'); require_once('include/func.php'); require_once('include/class_header.php'); function GPSOnline ($gpsonline){ $gpsonline= getHTTPGPSOnline(); $fstep = explode("|", $gpsonline); foreach($fstep as $k => $v){ if(strlen($v)>0){ $sstep [] = explode(",", $v);// создаем 2у мерный массив } } $array = array(); foreach ($sstep as $v){ $tmp['login'] = $v[0]; $tmp['ch'] = substr($v[0],0,1); $tmp['poz'] = substr($v[0],1); $tmp['la'] = $v[1]; $tmp['lo'] = $v[2]; $array[] = $tmp;// ключем нового многомерного масиива явлеться 0 элемент массива $sstep } echo json_encode($array); }} GPSOnline(); 

Everything works fine and gives me the necessary data! Next, this function was accessed by an AJAX request in the parameters of which was specified:

  url: "/1.php" 

Question : How can I transfer my 1.php function to the newly created method in the laravel framework and change the url value in the AJAX request (ie, change url: "/1.php" to url:"ссылка на метод laravel" ?

After sitting on the forums I registered a new class and added a method to it:

  public function GPSOnline () { $gpsonline=File::get( Config::get('app.driverspath')); $fstep = explode("|", $gpsonline); foreach($fstep as $k => $v){ if(strlen($v)>0){ $sstep [] = explode(",", $v); } } $array = array(); foreach ($sstep as $v){ $tmp['ch'] = substr($v[0],0,1); $tmp['poz'] = substr($v[0],1); $tmp['la'] = $v[1]; $tmp['lo'] = $v[2]; $array[] = $tmp; } // echo json_encode($array); return Response::json(array($array)); } 

In the AJAX request, I wrote to the source url:

 url:"{{URL::action('GpsController@GPSOnline')}}" 

But it still does not work, it seems to me that I did not correctly register the GPSOnline method ?!

What needs to be fixed?

  • Does nobody know? - CBETOBuT

2 answers 2

  • First, figure out your functions - with the help of composer, add your files to autoload or refactor code of files that are included and create a class based on your functions;
  • create a сontroller/action in which you use the functionality of the resulting code;
  • Next, create a route in app/routes.php with the url that will be used to address your controller/action :

     Route::ajax('some/route', 'ControllerName@ActionName'); 

basically everything

  1. check the route in the route (80% of the errors are there!)
  2. Be sure to perform checks, because the File::get method if the file is empty returns NULL !

Eventually:

  public function GPSOnline () { $gpsonline=File::get( Config::get('app.driverspath')); $tmp=array(); $sstep=array(); if ($gpsonline != NULL){ $fstep = explode("|", $gpsonline); foreach($fstep as $k => $v){ if(strlen($v)>0){ $sstep [] = explode(",", $v); } } $array = array(); foreach ($sstep as $v){ if(isset ($v[0])&&($v[1])&&($v[2])){ $tmp['ch'] = substr($v[0],0,1); $tmp['poz'] = substr($v[0],1); $tmp['la'] = $v[1]; $tmp['lo'] = $v[2]; $array[] = $tmp; } } return Response::json($array); } else { return Response::json (array()); } }