Within QGIS 3.0 and above updates took place within the API, 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 has also been answered in Was QgsMapLayerRegistry being removed in QGIS 3?