public function getController(Request $request) { $url = new URL($request->getPathInfo()); $one = $url->getArgument(1); $two = $url->getArgument(2); $controller = 'default'; $action = 'index'; if ($this->checkControllerFileExists($one)) { $controller = $one; if ($two) { $action = $two; } } else { if (!$this->checkControllerFileExists($controller)) { throw new HttpException(404, $controller . 'Controller.php не существует'); } if ($one) { $action = $one; } } $callable = $this->createController($controller, $action, $request, $url); if (!is_callable($callable)) { throw new HttpException(404, sprintf('Метод %sAction в контроллере %sController не найден', $action, $controller)); } return $callable; }
function testGetArgument() { $u = new URL('one', 'two'); $this->assertTrue($u->hasArgument(1), 'Аргумент №1 есть'); $this->assertEquals('one', $u->getArgument(1), 'Значение аргумента №1'); $this->assertTrue($u->hasArgument(2), 'Аргумент №2 есть'); $this->assertEquals('two', $u->getArgument(2), 'Значение аргумента №2'); $this->assertFalse($u->hasArgument(3), 'Аргумента №3 нет'); $this->assertEquals('hi', $u->getArgument(3, 'hi'), 'Значение по-умолчанию'); }
/** Определяем контроллер и действие */ protected function processURL() { $one = $this->url->getArgument(1); $two = $this->url->getArgument(2); $this->controller = 'default'; $this->action = 'index'; if ($this->checkControllerFileExists($one)) { $this->controller = $one; if ($two) { $this->action = $two; } } else { if (!$this->checkControllerFileExists($this->controller)) { throw new Exception($this->controller . 'Controller не существует', Exception::NOT_FOUND); } if ($one) { $this->action = $one; } } }
function testLoad() { //При запуске не из браузера $_SERVER['REQUEST_URI'] не существует $u = new URL(); $u->load(); $this->assertFalse($u->getArgument(1), 'Без REQUEST_URI пустой load = главная страница'); $exp = array(1 => 'one', 'two', 'three'); $u = new URL(); $u->load('one', 'two', 'three'); $this->assertEquals($exp, $u->getArguments(), 'Аргументы #1'); $this->assertTrue(is_array($u->getParameters()), 'Параметры - массив'); $this->assertEquals(0, count($u->getParameters()), 'Массив параметров пустой'); $p1 = array('hello' => 'world'); $p2 = array('world' => 'hello'); $u = new URL(); $u->load('/one/two/', 'three', $p1, $p2); $this->assertEquals($exp, $u->getArguments(), 'Аргументы #2'); $this->assertEquals(array_merge($p1, $p2), $u->getParameters(), 'Массив параметров'); $u = new URL(); $u->load('/one/two/', 'three', '/hello:world/', $p2); $this->assertEquals($exp, $u->getArguments(), 'Аргументы #2'); $this->assertEquals(array_merge($p1, $p2), $u->getParameters(), 'Массив параметров'); }