Answer the question
In order to leave comments, you need to log in
Why such an error when creating a custom module in Drupal?
I need to create a custom Drupal module that displays the weather on the page (three values: temperature, pressure, city) like this:
17.52
1016
London
But I have a problem with Drupal swearing at the php file. Although the same file not in Drupal project works as it should
. First I created:
1. weather.info.yml
2. weather.routing.yml
in
src / Controller folder I created WeatherPage.php file
name: Weather module
description: Weather module
core: 8.x
type: module
package: Weather
weather.weather_page:
path: '/weather'
defaults:
_controller: '\Drupal\weather\Controller\WeatherPage::getWeather'
requirements:
_permission: 'access content'
<?php
namespace Drupal\weather\Controller;
class WeatherPage
{
public function getWeather($city)
{
$response = file_get_contents('http://api.openweathermap.org/data/2.5/weather?q='.$city.',uk&appid=32ae008b1c7259324aa50450687fabf5&units=metric');
$jsn = json_decode($response);
echo $jsn->main->temp;
echo '<br>';
echo $jsn->main->pressure;
echo '<br>';
echo $city;
}
}
$class = new WeatherPage();
$class->getWeather('London');
?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question