Within QGIS 3.0 and above updates took place within the [API][1], `QgsMapLayerRegistry` has been removed and its functionality has been moved to `QgsProject`.

As a beginner, I found it confusing because they didn't make an update for the Developer Cookbook pdf version. This this has also been answered in https://gis.stackexchange.com/questions/244452/is-qgsmaplayerregistry-removed-in-qgis3

  [1]: https://qgis.org/api/api_break.html