This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Controllers/Controller.php

56 lines
1.2 KiB
PHP
Raw Normal View History

2016-02-15 21:20:46 +00:00
<?php
/**
* Holds the base controller.
* @package Sakura
*/
namespace Sakura\Controllers;
/**
* Base controller (which other controllers should extend on).
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class Controller
{
2016-08-05 02:35:37 +00:00
/**
* Middleware to execute upon creating this class.
* @var array
*/
2016-07-30 13:48:09 +00:00
protected $middleware = [
'UpdateLastOnline',
];
2016-08-05 02:35:37 +00:00
/**
* Used to except middleware in controllers that extend this one.
* @var array
*/
2016-07-30 13:48:09 +00:00
protected $exceptMiddleware = [];
2016-08-05 02:35:37 +00:00
/**
* Constructor.
*/
2016-07-30 13:48:09 +00:00
public function __construct()
{
// filter excepted middlewares
$middlewares = array_diff($this->middleware, $this->exceptMiddleware);
foreach ($middlewares as $middleware) {
$className = "Sakura\\Middleware\\{$middleware}";
(new $className)->run();
}
}
2016-08-05 02:35:37 +00:00
/**
* Encodes json.
* @param array|\stdObject $object
* @param int $operators
* @return string
*/
2016-12-04 16:33:52 +00:00
public function json($object, int $operators = null): string
2016-03-30 09:09:58 +00:00
{
2016-03-30 21:30:15 +00:00
header('Content-Type: application/json; charset=utf-8');
2016-07-31 01:32:37 +00:00
return json_encode($object, $operators);
2016-03-30 09:09:58 +00:00
}
2016-02-15 21:20:46 +00:00
}