Fix errors in the router

This commit is contained in:
Gregorio Chiko Putra 2017-09-04 11:02:25 +07:00
parent 128d337383
commit bfdd632adf
4 changed files with 21 additions and 8 deletions

View File

@ -9,7 +9,7 @@ class Posts
return true; return true;
} }
public function new() public function entry()
{ {
echo "You can entry new data here."; // Nanti di replace sama twig view ke App\Views\Data\new_pengumuman.html echo "You can entry new data here."; // Nanti di replace sama twig view ke App\Views\Data\new_pengumuman.html
return true; return true;

View File

@ -33,6 +33,7 @@ class Router
public function match($url) public function match($url)
{ {
$url = htmlspecialchars($url); $url = htmlspecialchars($url);
$url = substr_replace($url, '', 0, 1);
foreach ($this->routes as $route => $params) { foreach ($this->routes as $route => $params) {
if (preg_match($route, $url, $matches)) { if (preg_match($route, $url, $matches)) {

View File

@ -6,6 +6,8 @@ class RouterTest extends \PHPUnit\Framework\TestCase
/** /**
* Data provider for matchingRouteSuccess * Data provider for matchingRouteSuccess
* *
* Contains: route, params, $_SERVER['REQUEST_URI'], expected_params
*
* @return array * @return array
*/ */
public function routeProvider() public function routeProvider()
@ -13,15 +15,15 @@ class RouterTest extends \PHPUnit\Framework\TestCase
return [ return [
[ [
'', '',
['controller' => 'home', 'action' => 'index'], ['controller' => 'posts', 'action' => 'index'],
'', '',
['controller' => 'home', 'action' => 'index'] ['controller' => 'posts', 'action' => 'index']
], ],
[ [
'{controller}/{action}', '{controller}/{action}',
[], [],
'posts/new', 'posts/entry',
['controller' => 'posts', 'action' => 'new'] ['controller' => 'posts', 'action' => 'entry']
], ],
[ [
'{controller}/{action}/{id:\d+}', '{controller}/{action}/{id:\d+}',

View File

@ -1,9 +1,19 @@
<?php <?php
$router = new Router(); spl_autoload_register(function($class)
{
$root = dirname(__DIR__);
if(is_readable($file = $root . '/' . str_replace('\\', '/', $class) . '.php'))
{
require $file;
}
});
$router->add('', ['controller' => 'Data', 'action' => 'index']); $router = new Core\Router();
$router->add('', ['controller' => 'posts', 'action' => 'index']);
$router->add('{controller}/{action}'); $router->add('{controller}/{action}');
$router->add('{controller}/{action}/{id:\d+}'); $router->add('{controller}/{action}/{id:\d+}');
$router->add('{action}', ['controller' => 'home']);
$url = $_SERVER['QUERY_STRING']; $url = $_SERVER['REQUEST_URI'];
$router->dispatch($url); $router->dispatch($url);