Skip to content

Instantly share code, notes, and snippets.

@gregmercer
Created January 16, 2019 16:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gregmercer/d1734a13f334a036e18e1ae22344125f to your computer and use it in GitHub Desktop.
D8 - Services Example
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