In web development, handling time and date-related functions is a very common requirement. If you want to implement a service in Laravel that querys sunset time by city, you can implement it by encapsulating the date_sunset() function. Laravel provides powerful scalability, making modularizing such features very simple. In this article, we will explain step by step how to implement this function.
date_sunset() is a built-in function in PHP. It can return the sunset time of the place based on the given latitude and longitude, date and other parameters. The basic usage is as follows:
date_sunset(time(), SUNFUNCS_RET_STRING, $latitude, $longitude, 0, 0);
Where $latitude and $longitude are the latitude and longitude of the city. date_sunset() will return the corresponding sunset time based on this information.
In Laravel, we can encapsulate the date_sunset() function by creating a service class, thereby simplifying operations and enhancing the reusability and maintainability of the code.
First, in Laravel, we need to create a service class to encapsulate this function. This service class can be generated through the Artisan command:
php artisan make:service SunsetService
Then, we implement encapsulation logic in the app/Services/SunsetService.php file:
namespace App\Services;
class SunsetService
{
/**
* Get the city's sunset time
*
* @param string $cityName City name
* @return string
*/
public function getSunsetTimeByCity(string $cityName): string
{
// Obtain the latitude and longitude information of the city through the city name
$coordinates = $this->getCoordinatesByCity($cityName);
if (!$coordinates) {
return 'Unable to obtain city information';
}
// Get the sunset time
return date_sunset(time(), SUNFUNCS_RET_STRING, $coordinates['latitude'], $coordinates['longitude'], 0, 0);
}
/**
* 根据City name获取城市的经纬度
*
* @param string $cityName City name
* @return array|null
*/
private function getCoordinatesByCity(string $cityName)
{
// Calling external API Obtain urban latitude and longitude data
$url = "https://api.openweathermap.org/data/2.5/weather?q={$cityName}&appid=your_api_key";
// Obtain the latitude and longitude data of the city
$response = file_get_contents($url);
$data = json_decode($response, true);
// If the request is successful and there is latitude and longitude data,Return to latitude and longitude
if ($data && isset($data['coord'])) {
return [
'latitude' => $data['coord']['lat'],
'longitude' => $data['coord']['lon']
];
}
return null;
}
}
In the SunsetService service class, we provide two main methods:
getSunsetTimeByCity() : Receives a city name and returns the sunset time of the city.
getCoordinatesByCity() : Use an external API (such as OpenWeatherMap API) to get the latitude and longitude of the city based on the name of the city.
In getCoordinatesByCity() , we use the OpenWeatherMap API to obtain the latitude and longitude information of the city. In practice, you need to replace the API key that your_api_key applies for yourself.
Next, we need a controller to call the SunsetService service and return the result to the front-end. We can generate the controller by:
php artisan make:controller SunsetController
Then, use the service in app/Http/Controllers/SunsetController.php :
namespace App\Http\Controllers;
use App\Services\SunsetService;
use Illuminate\Http\Request;
class SunsetController extends Controller
{
protected $sunsetService;
public function __construct(SunsetService $sunsetService)
{
$this->sunsetService = $sunsetService;
}
/**
* Check the city's sunset time
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function getSunsetTime(Request $request)
{
$city = $request->get('city', 'Beijing');
$sunsetTime = $this->sunsetService->getSunsetTimeByCity($city);
return response()->json([
'city' => $city,
'sunset_time' => $sunsetTime
]);
}
}
Next, add a route for the controller in routes/web.php :
use App\Http\Controllers\SunsetController;
Route::get('/sunset', [SunsetController::class, 'getSunsetTime']);
Through this route, users can access /sunset?city=Beijing through their browser to check the sunset time in Beijing.
Now you have successfully encapsulated the date_sunset() function and created a service that querys sunset time by city. You can test this feature by visiting the following URL:
http://m66.net/sunset?city=Beijing
The returned JSON data will contain the sunset time of the city.
By encapsulating the date_sunset() function, we can easily implement the function of querying sunset times by city in Laravel. You can further expand this service according to specific needs, such as adding a caching mechanism, supporting different date queries, etc. Hope this article helps you better understand how to implement this feature in Laravel!