本文整理汇总了PHP中Symfony\Component\Routing\Matcher\UrlMatcher类的典型用法代码示例。如果您正苦于以下问题:PHP UrlMatcher类的具体用法?PHP UrlMatcher怎么用?PHP UrlMatcher使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UrlMatcher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
/**
* Run Application.
*/
public function run()
{
if (empty($this['request'])) {
$this['request'] = Request::createFromGlobals();
}
$routes = $this['routes'];
$request = $this['request'];
$context = $this['requestContext'];
$context->fromRequest($this['request']);
$matcher = new UrlMatcher($routes, $context);
$resolver = new ControllerResolver();
try {
$request->attributes->add($matcher->match($request->getPathInfo()));
$controller = $resolver->getController($request);
$arguments = $resolver->getArguments($request, $controller);
if (is_array($controller) && $controller[0] instanceof BaseController) {
$controller[0]->setRequest($request);
$controller[0]->setApplication($this);
}
$response = call_user_func_array($controller, $arguments);
} catch (ResourceNotFoundException $e) {
$response = new Response('Not Found', 404);
} catch (\Exception $e) {
$response = new Response('An error occurred', 500);
}
return $response;
}
示例2: getRoute
protected function getRoute()
{
$routeConfig = $this->application->getConfig('routes');
$context = new RequestContext('/');
$matcher = new UrlMatcher($routeConfig, $context);
return $matcher->match($_SERVER['REQUEST_URI']);
}
示例3: dispatch
/**
* @param boolean $bool
* @param \WP $wp
*/
public function dispatch($bool, \WP $wp)
{
// get path info after domain part of the URL
$pathinfo = $this->request->getPathInfo();
// try to match for stored routes
$context = new RequestContext('/');
$matcher = new UrlMatcher($this->routes, $context);
try {
$parameters = $matcher->match($pathinfo);
// without WordPress theme around
if ('basic_output' == $parameters['type']) {
$parameters['handler']($this->request, $parameters);
$this->handleExit();
// with WordPress theme around
} elseif ('page' == $parameters['type']) {
do_action('parse_request', $wp);
$this->setupQuery($parameters['page'], $this->request, $parameters);
do_action('wp', $wp);
$this->loader->load();
$this->handleExit();
}
} catch (ResourceNotFoundException $e) {
// route not found. we dont bother for now, because the request will be handled by WordPress which will show
// something like a page not found page.
}
return $bool;
}
示例4: testMatch
public function testMatch()
{
// test the patterns are matched are parameters are returned
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{bar}'));
$matcher = new UrlMatcher($collection, array(), array());
try {
$matcher->match('/no-match');
$this->fail();
} catch (NotFoundException $e) {
}
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
// test that defaults are merged
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
$matcher = new UrlMatcher($collection, array(), array());
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
// test that route "method" is ignored if no method is given in the context
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo', array(), array('_method' => 'GET|head')));
$matcher = new UrlMatcher($collection, array(), array());
$this->assertInternalType('array', $matcher->match('/foo'));
// route does not match with POST method context
$matcher = new UrlMatcher($collection, array('method' => 'POST'), array());
try {
$matcher->match('/foo');
$this->fail();
} catch (MethodNotAllowedException $e) {
}
// route does match with GET or HEAD method context
$matcher = new UrlMatcher($collection, array('method' => 'GET'), array());
$this->assertInternalType('array', $matcher->match('/foo'));
$matcher = new UrlMatcher($collection, array('method' => 'HEAD'), array());
$this->assertInternalType('array', $matcher->match('/foo'));
}
示例5: listener
public static function listener(RequestEvent $requestEvent)
{
$request = $requestEvent->getRequest();
$pathInfo = $request->getPathInfo();
$session = $requestEvent->getSession();
$configSecurity = $requestEvent->getSecurityConfig();
$routes = $requestEvent->getRoutes();
$context = new RequestContext();
$matcher = new UrlMatcher($routes, $context);
$context->fromRequest($request);
$matching = $matcher->match($pathInfo);
$matchedRoute = $matching['_route'];
if (isset($configSecurity['protected'])) {
$protected = $configSecurity['protected'];
$protectedRoutes = $protected['routes'];
$sessionKey = $protected['session'];
$notLoggedRoute = $protected['not_logged'];
$loggedRoute = $protected['logged'];
$redirectRoute = null;
if ($session->get($sessionKey) && $matchedRoute === $notLoggedRoute) {
$redirectRoute = $routes->get($loggedRoute);
}
if (!$session->get($sessionKey) && in_array($matchedRoute, $protectedRoutes)) {
$redirectRoute = $routes->get($notLoggedRoute);
}
if ($redirectRoute) {
$redirectResponse = new RedirectResponse($request->getBaseUrl() . $redirectRoute->getPath());
$redirectResponse->send();
}
}
}
示例6: handle
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
try {
$this->request = $request;
$pathInfo = $request->getPathInfo();
$this->loadRoutes($pathInfo);
$this->loadGeneralConfig();
$this->loadZyncroAppConfig($pathInfo);
$this->loadDatabaseConfig($pathInfo);
$this->loadSecurityConfig($pathInfo);
$this->loadTwig($pathInfo);
$this->loadUtils();
$this->method = $request->getMethod();
$this->dispatcher->dispatch('request', new RequestEvent($request, $this->session, $this->securityConfig, $this->routes));
$this->loadApi();
$context = new Routing\RequestContext();
$matcher = new Routing\Matcher\UrlMatcher($this->routes, $context);
$context->fromRequest($request);
$request->attributes->add($matcher->match($request->getPathInfo()));
$resolver = new ControllerResolver();
$controller = $resolver->getController($request);
$arguments = $resolver->getArguments($request, $controller);
$arguments[0] = $this;
$response = call_user_func_array($controller, $arguments);
} catch (Routing\Exception\ResourceNotFoundException $e) {
$response = new Response('Not Found', 404);
} catch (\Exception $e) {
$response = new Response('An error occurred: ' . $e->getMessage(), 500);
}
return $response;
}
示例7: match
/**
* @param string $pathinfo
*
* @return array
*/
public function match($pathinfo)
{
$urlMatcher = new UrlMatcher($this->getRouteCollection(), $this->getContext());
$result = $urlMatcher->match($pathinfo);
if (!empty($result)) {
// Remap locale for front-end requests
if ($this->isMultiDomainHost() && $this->isMultiLanguage() && !$result['preview']) {
$localeMap = $this->getLocaleMap();
$locale = $result['_locale'];
$result['_request_locale'] = $locale;
$result['_locale'] = $localeMap[$locale];
}
// Add extra parameters to parameter bag
$extraData = $this->domainConfiguration->getExtraData();
if (!empty($extraData)) {
$result['_extra'] = $extraData;
}
$nodeTranslation = $this->getNodeTranslation($result);
if (is_null($nodeTranslation)) {
throw new ResourceNotFoundException('No page found for slug ' . $pathinfo);
}
$result['_nodeTranslation'] = $nodeTranslation;
}
return $result;
}
示例8: match
/**
* Tries to match a URL path with a set of routes.
*
* If the matcher can not find information, it must throw one of the
* exceptions documented below.
*
* @param string $pathinfo The path info to be parsed (raw format, i.e. not
* urldecoded)
*
* @return array An array of parameters
*
* @throws ResourceNotFoundException If the resource could not be found
* @throws MethodNotAllowedException If the resource was found but the
* request method is not allowed
*/
public function match($pathinfo)
{
$urlMatcher = new UrlMatcher($this->routeCollection, $this->getContext());
$result = $urlMatcher->match($pathinfo);
if (!empty($result)) {
try {
// The route matches, now check if it actually exists
$result['content'] = $this->contentManager->findActiveBySlug($result['slug']);
} catch (NoResultException $e) {
try {
//is it directory index
if (substr($result['slug'], -1) == '/' || $result['slug'] == '') {
$result['content'] = $this->contentManager->findActiveBySlug($result['slug'] . 'index');
} else {
if ($this->contentManager->findActiveBySlug($result['slug'] . '/index')) {
$redirect = new RedirectResponse($this->request->getBaseUrl() . "/" . $result['slug'] . '/');
$redirect->sendHeaders();
exit;
}
}
} catch (NoResultException $ex) {
try {
$result['content'] = $this->contentManager->findActiveByAlias($result['slug']);
} catch (NoResultException $ex) {
throw new ResourceNotFoundException('No page found for slug ' . $pathinfo);
}
}
}
}
return $result;
}
示例9: resolveController
/**
* @return array|bool
*/
protected function resolveController()
{
try {
// doctrine mabo jambo to prepare route detection
$context = new RequestContext();
$context->fromRequest($this->request);
$matcher = new UrlMatcher($this->routeCollection, $context);
// try detecting controller & stuff
$this->request->attributes->add($matcher->match($this->request->getPathInfo()));
// resolve controller
$resolver = new ControllerResolver();
$controller = $resolver->getController($this->request);
$controller = $this->assembleController($controller);
// adding request and response variables to the controller
if (!empty($controller[0]) && $controller[0] instanceof AbstractSimplexController) {
$controller[0]->setRequest($this->request)->setResponse($this->response);
} else {
// or as attributes for a 'function' controller
$req = array('request' => $this->request, 'response' => $this->response);
$this->request->attributes->add($req);
}
// parsing arguments for the request and adding a last argument the request parameter itself
$arguments = $resolver->getArguments($this->request, $controller);
return array($controller, $arguments);
} catch (ResourceNotFoundException $e) {
}
return false;
}
示例10: run
function run()
{
$routes = new RouteCollection();
foreach ($this->serviceManager->get('ConfigManager')->getConfig('routes') as $rota => $values) {
$routes->add($rota, new Route($values['route'], array('controller' => $values['controller'], 'action' => $values['action'])));
}
$context = $this->serviceManager->get('RequestManager')->getContext();
$matcher = new UrlMatcher($routes, $context);
$errorController = $this->getServiceManager()->get('ErrorController');
try {
$parameters = $matcher->match($context->getPathInfo());
$controller = $this->getServiceManager()->get($parameters['controller']);
$action = $this->getNomeAction($parameters['action']);
if (false == method_exists($controller, $action)) {
throw new Exception(sprintf('O Controller %s não possui o método %s', get_class($controller), $action));
}
$actionParameters = $this->getActionParameters($parameters);
if (true == method_exists($controller, 'beforeDispatch')) {
call_user_func(array($controller, 'beforeDispatch'));
}
return call_user_func_array(array($controller, $action), $actionParameters);
} catch (ResourceNotFoundException $ex) {
return $errorController->actionError404();
} catch (MethodNotAllowedException $ex) {
return $errorController->actionError500($ex);
} catch (Exception $ex) {
return $errorController->actionError500($ex);
}
}
示例11: run
/**
* Run Application.
*/
public function run()
{
$request = $this['request'] = Request::createFromGlobals();
$routes = $this['routes'];
$context = $this['request_context'];
$context->fromRequest($this['request']);
$matcher = new UrlMatcher($routes, $context);
$resolver = new ControllerResolver();
try {
$request->attributes->add($matcher->match($request->getPathInfo()));
$controller = $resolver->getController($request);
$arguments = $resolver->getArguments($request, $controller);
if (is_array($controller) && $controller[0] instanceof \Task\Controllers\BaseController) {
$controller[0]->setRequest($request);
$controller[0]->setApplication($this);
}
$response = call_user_func_array($controller, $arguments);
} catch (ResourceNotFoundException $e) {
$response = new JsonResponse(["errors" => ["type" => "Not found"]], Response::HTTP_NOT_FOUND);
} catch (\Exception $e) {
$response = new JsonResponse(["errors" => ["type" => $e->getMessage(), "stacktrace" => $e->getTraceAsString()]], Response::HTTP_INTERNAL_SERVER_ERROR);
}
$response->headers->set("Access-Control-Allow-Origin", "*");
$response->headers->set("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
$response->headers->set("Access-Control-Allow-Headers", "Content-Type");
$response->headers->set("Server", "Test task REST");
return $response;
}
示例12: match
public function match($urlOrRequest = null)
{
$request = $this->makeRequest($urlOrRequest);
$matcher = new UrlMatcher($this->getRoutes(), $this->context);
$params = $matcher->matchRequest($request);
return $this->handler->handle($params, $request);
}
示例13: match
public function match(Request $request)
{
$context = new RequestContext();
$context->fromRequest($request);
$this->context = $context;
$matcher = new UrlMatcher($this->getRouteCollection(), $context);
return $matcher->match($request->getPathInfo());
}
示例14: parseRequest
public function parseRequest(Event $event)
{
$request = $event->get('request');
$matcher = new UrlMatcher($this->routes, array('base_url' => $request->getBaseUrl(), 'method' => $request->getMethod(), 'host' => $request->getHost(), 'is_secure' => $request->isSecure()));
if (false === ($attributes = $matcher->match($request->getPathInfo()))) {
return false;
}
$request->attributes->add($attributes);
}
示例15: testSimpleRoute
public function testSimpleRoute()
{
$context = new RequestContext();
$request = Request::create("/hello");
$context->fromRequest($request);
$matcher = new UrlMatcher($this->routes, $context);
$parameters = $matcher->match('/hello');
$this->assertJsonStringEqualsJsonString('{"_route": "hello", "_controller": "foo"}', json_encode($parameters));
}