Skip to content

Instantly share code, notes, and snippets.

@mrded
Last active December 22, 2015 01:48
Show Gist options
  • Save mrded/6398654 to your computer and use it in GitHub Desktop.
Save mrded/6398654 to your computer and use it in GitHub Desktop.
Drupal 8 Controller Example
<?php
/**
* @file
* Contains \Drupal\session_test\Controller\ExampleController.
*/
namespace Drupal\example\Controller;
use Drupal\Core\ControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactory;
/* All global variables here. */
use Symfony\Component\HttpFoundation\Request;
/**
* Provides page callbacks for the configuration interface.
*/
class ExampleController implements ControllerInterface {
/**
* Stores the configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('config.factory'));
}
/**
* Constructs a object.
*/
public function __construct(ConfigFactory $config_factory) {
$this->configFactory = $config_factory;
}
/* @see: https://drupal.org/node/2032447 */
public function getGlobalUser() {
return Drupal::currentUser();
}
public function getGlobalCookie(Request $request) {
return $request->cookies->get(session_name());
}
public function getSession($key, Request $request) {
return $request->getSession()->get($key);
}
public function setSession($key, $value, Request $request) {
$session = $request->getSession();
$session->set($key, $value);
$request->setSession($session);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment