I created custom module following the Creating a custom module on Drupal tutorial.

The teacher of this tutorial demonstrated how you can create a custom module that displays on the page Hello world. But I have a slightly more difficult task. I need to create a custom module that will display the weather on the page, receiving data using API, like this.


What I did.

I created the weather.info.yml file.

name: Weather module              
description: Weather module 
core: 8.x                               
type: module                        
package: Weather  

I created the weather.routing.yml file.

  path: '/weather'
    _controller: '\Drupal\weather\Controller\WeatherPage::getWeather'                          
     _permission: 'access content'  

In src/Controller, I created the WeatherPage.php file.

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();


I get an error related to the controller.

enter image description here

How do I fix this error?

P.S. One forum user writes that the problem may be due to:

  • The controller must return the array
  • No code should be behind the class

I'm a Drupal newbie.

  • 2
    You are assuming the call to getWeather comes from within your class, where you are providing it with 'London'. However that function is also called from the route file, where you do not provide a value for city. Please clarify where the city should come from. Will you provide it from url, user profile, etc? Jun 19, 2020 at 18:45
  • @Neograph734 One forum user writes that the problem may be due to: 1) the controller must return the array 2) no code should be behind the class But I'm a newcomer to drupal. And php I do not know at all. Jun 19, 2020 at 19:01
  • Drupal is a bit difficult to get into, and since Drupal 8 there is much more smaller files all doing their specific parts. Rather than clicking along with a tutorial (which can be a great start) try to look up the relevant documentation to actually understand what is happening. In this case The routing system and specifically the section Parameters in routes. Jun 19, 2020 at 19:16
  • To fully stick with Drupal standards it would indeed be better to have the controller return a render array instead. Jun 19, 2020 at 19:18

1 Answer 1


You need to add the parameter to the path. Change this:

path: '/weather'

To this:

path: '/weather/{city}'

Note that the parameter name {city} must be expressed as the parameter $city in your callback (which you've done). If you for exampled changed the function definition to getWeather($c), you would get an error.

Also, you need to remove this code:

$class = new WeatherPage();


And finally, you'll need to return a render array. Change this:

echo $jsn->main->temp; 

To this:

return [
  '#markup' => $jsn->main->temp,
  • When I wrote as you advised: i.piccy.info/i9/a798a59ca6461d27efaa8e4ee4846dbc/1592593871/… I get error: i.piccy.info/i9/a8911222b1a7b1d6806171a5bbc9cd89/1592593982/… Maybe I misunderstood something? Then please write in more detail in the answer Jun 19, 2020 at 19:14
  • You need to clear the Drupal cache after making any changes to routing files.
    – Jaypan
    Jun 19, 2020 at 19:17
  • Also, {London} needs to be {city}. Then you will visit the path /weather/London to get London weather.
    – Jaypan
    Jun 19, 2020 at 19:18
  • 1
    You need to return a better render array instead of just markup, one with your desired items and fed to a theme function.
    – Kevin
    Jun 19, 2020 at 20:48
  • 3
    The formatting of your render array is a new question. This one was about the error accessing your Controller, which has been answered.
    – Jaypan
    Jun 20, 2020 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.