<?php
namespace Trap\ViewHandle;
class View
{
static public function view(array $template = [], array $v = [])
{
extract($v);
require_once __DIR__ . '/../../views/index.php';
}
}
\Trap\ViewHandle\View::view(['signup']);
<?php
namespace Trap\Routing;
use Trap\Http\Request;
class Router //extends Request
{
use Request;
public $app;
public $routes = [];
public $uri;
public $method;
public $action = null;
public $attributes = [];
public function __construct(\Trap\Application $app)
{
$this->app = $app;
}
public function group(array $attributes, \Closure $callback)
{
call_user_func($callback, $this);
}
protected function parseAction($action)
{
if (is_string($action) && mb_stripos($action, '@'))
{
$action = explode('@', $action);
$action[0] = 'controllers\\' . $action[0];
}
return $action;
}
protected function addRoute(string $method, string $uri, $action)
{
$action = $this->parseAction($action);
$uri = '/' . trim($uri, '/');
$this->routes[$method][] = ['uri' => $uri, 'action' => $action];
}
public function get(string $uri, $action)
{
$this->addRoute('GET', $uri, $action);
}
public function post(string $uri, $action)
{
$this->addRoute('POST', $uri, $action);
}
public function parseRoute()
{
$this->uri = $this->getUri();
$this->method = $this->getMethod();
if (isset($this->routes[$this->method]))
{
foreach ($this->routes[$this->method] as $route)
{
if ($route['uri'] === $this->uri)
{
$this->action = $route['action'];
}
else if (preg_match('#^' . $route['uri'] . '$#i', $this->uri, $matches))
{
$matches = array_slice($matches, 1);
$this->action = $route['action'];
$this->attributes = $matches;
}
}
}
}
public function handle()
{
if (is_array($this->action) && !is_null($this->action))
{
if (class_exists($this->action[0]) && method_exists($this->action[0], $this->action[1]))
{
$controller = new $this->action[0];
$action = $this->action[1];
call_user_func_array([$controller, $action], $this->attributes);
}
}
else if (is_callable($this->action) && !is_null($this->action))
{
$result = call_user_func_array($this->action, $this->attributes);
if (is_string($result) || is_int($result)) echo $result;
}
else
{
header("HTTP/1.0 404 Not Found");
exit;
}
}
public function run()
{
$this->parseRoute();
$this->handle();
}
}
for(var tM = 0; tM < tabMenu.children.length; tM++)
{
tabMenu.children[tM].children[0].onclick = function()
{
history.pushState(null, null, this.href);
getAjax('GET', location.pathname, null, getContent);
var getTitle = function()
{
if (xhr.readyState === 4)
{
if (xhr.status === 200)
{
var jText = JSON.parse(xhr.responseText);
document.title = jText.title;
}
else console.log(xhr.status + ' ' + xhr.statusText);
}
};
getAjax('GET', '/api' + location.pathname, null, getTitle);
return false;
}
}