Example #1
0
	/**
	 * Dispatches an HTTP request
	 *
	 * @param \rox\http\Request $request
	 * @throws \rox\http\DispatcherException
	 */
	public function dispatch(Request $request) {
		Normalizer::normalize($request);

		$route = $request->getQuery('route', '/');

		$params = Router::parseUrl($route, $request);
		if ($params === false) {
			throw new DispatcherException('No route matches request', 404);
		}

		$this->_loadController($params);

		$response = new Response;

		$controller = new $params['controller_class'](array(
			'request' => $request,
			'response' => $response
		));

		$controller->params = $params;

		if (!method_exists($controller, $params['action_method']) ||
			!is_callable(array($controller, $params['action_method']))) {
			throw new DispatcherException('Action does not exist or is not dispatchable', 404);
		}

		$controller->beforeFilter();
		call_user_func_array(array($controller, $params['action_method']), $params['args']);
		$controller->render();
		$controller->afterFilter();

		$controller->response->render();
	}
Example #2
0
 public function testParseUrl()
 {
     Router::resource('companies');
     Router::resource('companies.people');
     Router::connect('/:controller/:action/:id', array());
     Router::connect('/:controller/:action', array());
     Router::connect('/:controller', array('action' => 'index'));
     $_SERVER['REQUEST_METHOD'] = 'GET';
     $result = Router::parseUrl('/companies', Request::fromGlobals());
     $expected = array('controller' => 'companies', 'controller_class' => 'CompaniesController', 'action' => 'index', 'action_method' => 'indexAction', 'args' => array(), 'namespace' => false, 'extension' => 'html');
     $this->assertEquals($expected, $result);
     $_SERVER['REQUEST_METHOD'] = 'POST';
     $result = Router::parseUrl('/companies', Request::fromGlobals());
     $expected = array('controller' => 'companies', 'controller_class' => 'CompaniesController', 'action' => 'add', 'action_method' => 'addAction', 'args' => array(), 'namespace' => false, 'extension' => 'html');
     $this->assertEquals($expected, $result);
     $_SERVER['REQUEST_METHOD'] = 'PUT';
     $result = Router::parseUrl('/companies/1');
     $expected = array('controller' => 'companies', 'controller_class' => 'CompaniesController', 'action' => 'edit', 'action_method' => 'editAction', 'args' => array('id' => '1'), 'namespace' => false, 'extension' => 'html');
     $this->assertEquals($expected, $result);
     $_SERVER['REQUEST_METHOD'] = 'DELETE';
     $result = Router::parseUrl('/companies/1', Request::fromGlobals());
     $expected = array('controller' => 'companies', 'controller_class' => 'CompaniesController', 'action' => 'delete', 'action_method' => 'deleteAction', 'args' => array('id' => '1'), 'namespace' => false, 'extension' => 'html');
     $this->assertEquals($expected, $result);
     $_SERVER['REQUEST_METHOD'] = 'GET';
     $result = Router::parseUrl('/companies/1.json', Request::fromGlobals());
     $expected = array('controller' => 'companies', 'controller_class' => 'CompaniesController', 'action' => 'view', 'action_method' => 'viewAction', 'args' => array('id' => '1'), 'namespace' => false, 'extension' => 'json');
     $this->assertEquals($expected, $result);
     $_SERVER['REQUEST_METHOD'] = 'GET';
     $result = Router::parseUrl('/companies/1/people/25.xml', Request::fromGlobals());
     $expected = array('controller' => 'people', 'controller_class' => 'PeopleController', 'action' => 'view', 'action_method' => 'viewAction', 'args' => array('company_id' => '1', 'id' => '25'), 'namespace' => false, 'extension' => 'xml');
     $this->assertEquals($expected, $result);
     $_SERVER['REQUEST_METHOD'] = 'GET';
     $result = Router::parseUrl('/users/edit/23', Request::fromGlobals());
     $expected = array('controller' => 'users', 'controller_class' => 'UsersController', 'action' => 'edit', 'action_method' => 'editAction', 'args' => array('id' => '23'), 'namespace' => false, 'extension' => 'html');
     $this->assertEquals($expected, $result);
 }