H
H
hulktoster2020-06-19 20:47:29
PHP
hulktoster, 2020-06-19 20:47:29

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.routing.yml code:
weather.weather_page:                              
  path: '/weather'
  defaults:                                                      
    _controller: '\Drupal\weather\Controller\WeatherPage::getWeather'                          
  requirements:                                        
     _permission: 'access content'


WeatherPage.php code:
<?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');

?>


But I have this displayed on the page:

5eecfa1b8c497048938575.png

What did I write wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question