NWcoding
NWcoding

Reputation: 39

laravel 4.2 How to convert my php codes into laravel standards

I'm trying to convert my php codes to laravel standard, I want to add this to my controller and render it to my view. This php code works fine it's function is to determine the distance between two points. I just want to convert this php codes to laravel standards, in most simple way. Anyone knows how to do it? Thanks in advance. Here's my codes:

<?php
function getdistance($latrider,$lontrider,$latdriver,$londriver){
    $earthRadius=6371;
    $latFrom=deg2rad($latrider);
    $lonFrom=deg2rad($lontrider);

    $latTo=deg2rad($latdriver);
    $lonTo=deg2rad($londriver);

    $latDelta=$latTo-$latFrom;
    $lonDelta=$lonTo-$lonFrom;

    $angle=2*asin(sqrt(pow(sin($latDelta/2),2)+
        cos($latFrom)*cos($latTo)*pow(sin($lonDelta/2),2)));
    return $angle*$earthRadius;
}

$latrider=11.707389;
$lontrider=122.37194309999995;

$latdriver=11.7105254;
$londriver=122.36308980000001;

$distance=getDistance($latrider,$lontrider,$latdriver,$londriver);
echo"distance between rider position and driver position is:".$distance."KM";
?>

Upvotes: 1

Views: 79

Answers (1)

thiout_p
thiout_p

Reputation: 825

The code in your controller should look like :

<?php namespace App\Http\Controllers;

use App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class YourController extends Controller {

    private function getdistance($latrider,$lontrider,$latdriver,$londriver) {
        $earthRadius=6371;
        $latFrom=deg2rad($latrider);
        $lonFrom=deg2rad($lontrider);

        $latTo=deg2rad($latdriver);
        $lonTo=deg2rad($londriver);

        $latDelta=$latTo-$latFrom;
        $lonDelta=$lonTo-$lonFrom;

        $angle=2*asin(sqrt(pow(sin($latDelta/2),2)+
            cos($latFrom)*cos($latTo)*pow(sin($lonDelta/2),2)));
        return $angle*$earthRadius;
    }

    public function index (Request $request) {

        $latrider=11.707389;
        $lontrider=122.37194309999995;

        $latdriver=11.7105254;
        $londriver=122.36308980000001;

        $data = array( 
            'distance' => $this->getDistance($latrider,$lontrider,$latdriver,$londriver)
        );

        return view('your-view')->with($data);
    }

}

You also need to add the corresponding route in the App/Http/route.php :

 Route::get( '/your-route', ['uses' => 'YourController@index']);

Upvotes: 1

Related Questions