Skip to content

Instantly share code, notes, and snippets.

@jaromirnyklicek
Created March 24, 2014 08:17
Show Gist options
  • Save jaromirnyklicek/9736216 to your computer and use it in GitHub Desktop.
Save jaromirnyklicek/9736216 to your computer and use it in GitHub Desktop.
<?php
class DbRoute extends Object implements IRouter
{
protected $absolute = FALSE;
public function __construct($absolute = FALSE)
{
$this->absolute = $absolute;
}
public function match(IHttpRequest $httpRequest)
{
$url = $httpRequest->getUri();
$urlParts = explode($url->scriptPath, $url->path);
$result = dibi::fetch('SELECT * FROM urls WHERE slug = %s', $urlParts[1]);
$params = array();
if ($result) {
$params['url'] = $result->slug;
if ($result->product) {
$presenter = 'Front:Product';
$params['action'] = 'detail';
} elseif ($result->category) {
$presenter = 'Front:Eshop';
$params['action'] = 'category';
} elseif ($result->text) {
$presenter = 'Front:Text';
$params['action'] = 'detail';
} else {
throw new BadRequestException;
}
$params += $httpRequest->getQuery();
return new PresenterRequest(
$presenter,
$httpRequest->getMethod(),
$params,
$httpRequest->getPost(),
$httpRequest->getFiles(),
array(PresenterRequest::SECURED => $httpRequest->isSecured())
);
} else {
return NULL;
}
}
public function constructUrl(PresenterRequest $appRequest, IHttpRequest $httpRequest)
{
$params = $appRequest->getParams();
if (!isset($params['url'])) {
return NULL;
} else {
$uri = $params['url'];
unset($params['url']);
unset($params['action']);
$sep = ini_get('arg_separator.input');
$query = http_build_query($params, '', $sep ? $sep[0] : '&');
if ($query != '') {
$uri .= '?' . $query;
}
$uri = $httpRequest->getUri()->getBaseUri() . $uri;
if ($this->absolute) {
// doimplementovat tvoreni absolutnich url
}
return $uri;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment