Created
January 16, 2019 16:48
-
-
Save gregmercer/d1734a13f334a036e18e1ae22344125f to your computer and use it in GitHub Desktop.
D8 - Services Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
D8 - Services | |
... | |
lightning-8/modules/custom/basic/basic.services.yml | |
services: | |
basic.basic_hero_articles: | |
class: Drupal\basic\BasicArticleService | |
... | |
lightning-8/modules/custom/basic/src/BasicArticleService.php | |
<?php | |
namespace Drupal\basic; | |
/** | |
* Our basic articles service | |
*/ | |
class BasicArticleService { | |
/** | |
* Methood for getting Articles, regarding heroes. | |
*/ | |
public function getHeroArticles() { | |
$articles = ["Article 1", "Article 2"]; | |
return $articles; | |
} | |
} | |
... | |
Test using devel/php page | |
$basicArticleService = Drupal::service("basic.basic_hero_articles"); | |
kint($basicArticleService->getHeroArticles()); die(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment