init
This commit is contained in:
112
src/Core/Controllers/Controller.php
Normal file
112
src/Core/Controllers/Controller.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace Kletellier\MiniWeb\Core\Controllers;
|
||||
|
||||
use Kletellier\MiniWeb\Core\Kernel\Templating;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
protected $_cookies;
|
||||
protected $_cookiestodelete;
|
||||
protected Request $_request;
|
||||
|
||||
/**
|
||||
* Controller constructor
|
||||
*
|
||||
* @param String $controller controller name
|
||||
* @param String $action action to execute
|
||||
*/
|
||||
function __construct()
|
||||
{
|
||||
$this->_cookies = array();
|
||||
$this->_cookiestodelete = array();
|
||||
$this->_request = Request::createFromGlobals();
|
||||
}
|
||||
|
||||
public function render($content, $status = 200, $headers = array('Content-Type' => 'text/html')): void
|
||||
{
|
||||
$this->getResponse($content, $status, $headers)->send();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return instance of Symfony Response Component
|
||||
* @return type
|
||||
*/
|
||||
private function getResponse($content, $status = 200, $headers = array('Content-Type' => 'text/html')): Response
|
||||
{
|
||||
$response = new Response($content, $status, $headers);
|
||||
foreach ($this->_cookies as $cookie) {
|
||||
$response->headers->setCookie($cookie);
|
||||
}
|
||||
foreach ($this->_cookiestodelete as $cookie) {
|
||||
$response->headers->clearCookie($cookie);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return instance of Symfony Request component
|
||||
*/
|
||||
public function request(): Request
|
||||
{
|
||||
return $this->_request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return Html from view
|
||||
*/
|
||||
public function renderView($view, $params = []): string
|
||||
{
|
||||
return Templating::getInstance()->render($view, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add cookie to response
|
||||
* @param type \Symfony\Component\HttpFoundation\Cookie $cookie
|
||||
* @return void
|
||||
*/
|
||||
public function addCookie(\Symfony\Component\HttpFoundation\Cookie $cookie)
|
||||
{
|
||||
array_push($this->_cookies, $cookie);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cookie to remove in response
|
||||
* @param string $cookie cookie name to delete
|
||||
* @return void
|
||||
*/
|
||||
public function removeCookie($cookie)
|
||||
{
|
||||
array_push($this->_cookiestodelete, $cookie);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a Json Response
|
||||
*
|
||||
* @param object $var object to be serialized in JSON
|
||||
*/
|
||||
public function renderJSON($var)
|
||||
{
|
||||
$response = new Response(json_encode($var));
|
||||
$response->headers->set('Content-Type', 'application/json');
|
||||
$response->send();
|
||||
}
|
||||
|
||||
public function redirecting($url)
|
||||
{
|
||||
$response = new \Symfony\Component\HttpFoundation\RedirectResponse($url);
|
||||
foreach ($this->_cookies as $cookie) {
|
||||
$response->headers->setCookie($cookie);
|
||||
}
|
||||
foreach ($this->_cookiestodelete as $cookie) {
|
||||
$response->headers->clearCookie($cookie);
|
||||
}
|
||||
$response->send();
|
||||
}
|
||||
|
||||
function __destruct()
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user