/** * @inheritdoc */ public function assemble(array $params = array(), array $options = array()) { foreach ($params as $key => &$value) { if (in_array($key, $this->_hashidsParams)) { $value = $this->_encode($value); } } return parent::assemble($params, $options); }
public function __construct($route, array $constraints = [], array $defaults = []) { parent::__construct($route, $constraints, $defaults); // add the slash to the allowed unencoded chars map, since this route // includes that charater in its 'page' parameter if (!isset(static::$urlencodeCorrectionMap['%2F'])) { static::$urlencodeCorrectionMap['%2F'] = '/'; } }
public function setUp() { $router = new SimpleRouteStack(); $router->addRoute('home', LiteralRoute::factory(array('route' => '/', 'defaults' => array('controller' => 'ZendTest\\Mvc\\Controller\\TestAsset\\SampleController')))); $router->addRoute('sub', SegmentRoute::factory(array('route' => '/foo/:param', 'defaults' => array('param' => 1)))); $this->controller = new SampleController(); $this->request = new Request(); $this->event = new MvcEvent(); $this->routeMatch = new RouteMatch(array('controller' => 'controller-sample', 'action' => 'postPage')); $this->event->setRequest($this->request); $this->event->setRouteMatch($this->routeMatch); $this->event->setRouter($router); $this->sessionManager = new SessionManager(); $this->sessionManager->destroy(); $this->controller->setEvent($this->event); $this->controller->flashMessenger()->setSessionManager($this->sessionManager); }
public function match(Request $request, $pathOffset = null, array $options = array()) { $routeMatch = parent::match($request, $pathOffset, $options); if ($routeMatch instanceof RouteMatch) { $entityName = $routeMatch->getParam('entity'); $params = $routeMatch->getParams(); unset($params['entity'], $params['controller'], $params['action'], $params['headTitle'], $params['pageTitle'], $params['pageDescription']); /** @var EntityManager $objectManager */ $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $objectRepository = $objectManager->getRepository($entityName); $entity = $objectRepository->findOneBy($params); if (!$entity) { return null; } $routeMatch->setParam('entity', $entity); } return $routeMatch; }
public function setUp() { $this->form = new Form(); $this->collection = new Collection('links', array('count' => 1, 'allow_add' => true, 'target_element' => array('type' => 'ZendTest\\Mvc\\Controller\\Plugin\\TestAsset\\LinksFieldset'))); $router = new SimpleRouteStack(); $router->addRoute('home', LiteralRoute::factory(array('route' => '/', 'defaults' => array('controller' => 'ZendTest\\Mvc\\Controller\\TestAsset\\SampleController')))); $router->addRoute('sub', SegmentRoute::factory(array('route' => '/foo/:param', 'defaults' => array('param' => 1)))); $router->addRoute('ctl', SegmentRoute::factory(array('route' => '/ctl/:controller', 'defaults' => array('__NAMESPACE__' => 'ZendTest\\Mvc\\Controller\\TestAsset')))); $this->controller = new SampleController(); $this->request = new Request(); $this->event = new MvcEvent(); $this->routeMatch = new RouteMatch(array('controller' => 'controller-sample', 'action' => 'postPage')); $this->event->setRequest($this->request); $this->event->setRouteMatch($this->routeMatch); $this->event->setRouter($router); $this->sessionManager = new SessionManager(); $this->sessionManager->destroy(); $this->controller->setEvent($this->event); $this->controller->flashMessenger()->setSessionManager($this->sessionManager); }
private function generateRoute(MvcEvent $e) { if (!$this->cacheRouteData) { $sm = $e->getApplication()->getServiceManager(); $this->cacheRouteData = $sm->get('RouteData'); } try { $router = $e->getRouter(); foreach ($this->cacheRouteData as $data) { $constraints = json_decode($data['constraints'], true); $data['constraints'] = $constraints; $data['defaults'] = array('controller' => $data['controller'], 'action' => 'index'); $route = Segment::factory($data); $router->addRoute($data['name'], $route); } $this->hasRoute = true; } catch (\Exception $ex) { throw $ex; } }
/** * Get an initialized instance of the controller plugin * * If controller setup is requested, the controller will be a * \Library\Test\Mvc\Controller\TestController. Its MvcEvent will be * initialized with a standard route 'test' (/module/controller/action/) * with defaults of "defaultcontroller" and "defaultaction". * The RouteMatch is initialized with "currentcontroller" and * "currentaction". An empty response is created. * * @param bool $setController Initialize the helper with a working controller (default: TRUE) * @return \Zend\Mvc\Controller\Plugin\PluginInterface Plugin instance */ protected function _getPlugin($setController = true) { if ($setController) { $router = new \Zend\Mvc\Router\Http\TreeRouteStack(); $router->addRoute('test', \Zend\Mvc\Router\Http\Segment::factory(array('route' => '/[module[/]][:controller[/][:action[/]]]', 'defaults' => array('controller' => 'defaultcontroller', 'action' => 'defaultaction')))); $routeMatch = new \Zend\Mvc\Router\RouteMatch(array('controller' => 'currentcontroller', 'action' => 'currentaction')); $routeMatch->setMatchedRouteName('test'); $event = new \Zend\Mvc\MvcEvent(); $event->setRouter($router); $event->setRouteMatch($routeMatch); $event->setResponse(new \Zend\Http\Response()); // Register TestController with the service manager because this is // not done in the module setup $manager = Application::getService('ControllerManager'); $manager->setInvokableClass('test', 'Library\\Test\\Mvc\\Controller\\TestController'); $this->_controller = $manager->get('test'); $this->_controller->setEvent($event); return $this->_controller->plugin($this->_getPluginName()); } else { return $this->_getPluginManager()->get($this->_getPluginName()); } }
public function testAssemblingWithMissingParameterInRoot() { $this->setExpectedException('Zend\\Mvc\\Router\\Exception\\InvalidArgumentException', 'Missing parameter "foo"'); $route = new Segment(':foo'); $route->assemble(); }
public function testEncodedDecode() { // every character $in = '%61%62%63%64%65%66%67%68%69%6a%6b%6c%6d%6e%6f%70%71%72%73%74%75%76%77%78%79%7a%41%42%43%44%45%46%47%48%49%4a%4b%4c%4d%4e%4f%50%51%52%53%54%55%56%57%58%59%5a%30%31%32%33%34%35%36%37%38%39%60%2d%3d%5b%5d%5c%3b%27%2c%2e%2f%7e%21%40%23%24%25%5e%26%2a%28%29%5f%2b%7b%7d%7c%3a%22%3c%3e%3f'; $out = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`-=[]\\;\',./~!@#$%^&*()_+{}|:"<>?'; $request = new Request(); $request->setUri('http://example.com/' . $in); $route = new Segment('/:foo'); $match = $route->match($request); $this->assertSame($out, $match->getParam('foo')); }
/** * Match (for REST) * * Add a 'restAction' param to the RouteMatch object to indicate the action that will be taken by the * AbstractRestfulController * * @param Request $request * @param null $pathOffset * @param array $options * @return null|\Zend\Mvc\Router\Http\RouteMatch */ public function match(Request $request, $pathOffset = null, array $options = array()) { $routeMatch = parent::match($request, $pathOffset, $options); if ($routeMatch) { // If the route matched for this router then... $action = $routeMatch->getParam('action', false); if (!$action) { // Ignore actions that were already set, those get mapped to custom http methods. // RESTful methods $method = strtolower($request->getMethod()); switch ($method) { // DELETE case 'delete': $id = $this->getIdentifier($routeMatch, $request); if ($id !== false) { $action = 'delete'; break; } $action = 'deleteList'; break; // GET // GET case 'get': $id = $this->getIdentifier($routeMatch, $request); if ($id !== false) { $action = 'get'; break; } $action = 'getList'; break; // HEAD // HEAD case 'head': $action = 'head'; break; // OPTIONS // OPTIONS case 'options': $action = 'options'; break; // PATCH // PATCH case 'patch': $id = $this->getIdentifier($routeMatch, $request); if ($id !== false) { $action = 'patch'; break; } $action = 'patchList'; break; // POST // POST case 'post': $action = 'create'; break; // PUT // PUT case 'put': $id = $this->getIdentifier($routeMatch, $request); if ($id !== false) { $action = 'update'; break; } $action = 'replaceList'; break; // All others... // All others... default: $action = null; } } $routeMatch->setParam('restAction', $action); } return $routeMatch; }
<?php require "./vendor/autoload.php"; $route = \Zend\Mvc\Router\Http\Segment::factory(array('route' => '/:controller', 'constraints' => array('controller' => '[A-Za-z]+'))); $req = new \Zend\Http\Request(); $req->setUri($argv[1]); $match = $route->match($req); if ($match != null) { echo "OK"; } else { echo "NOn ok"; }
public function testAssemblingWithExistingChild() { $route = new Segment('/[:foo]', array(), array('foo' => 'bar')); $path = $route->assemble(array(), array('has_child' => true)); $this->assertEquals('/bar', $path); }
<?php require "vendor/autoload.php"; $route = \Zend\Mvc\Router\Http\Segment::factory(array('route' => '/:controller[/:toto]', 'constraints' => array('controller' => '[a-zA-Z][a-zA-Z0-9_-]+', 'toto' => '[a-zA-Z][a-zA-Z0-9_-]+'), 'defaults' => array('controller' => 'Application\\Controller\\IndexController', 'action' => 'index'))); $req = new \Zend\Http\Request(); $req->setUri('http://monsite/stock/inventaire'); $match = $route->match($req); if ($match !== null) { echo $match->getParam('toto') . "\n"; } else { echo "ressource non connue \n"; }
public function __construct($route, array $constraints = array(), array $defaults = array(), array $skippable = array()) { $this->skippable = $skippable; parent::__construct($route, $constraints, $defaults); }
/** * @param Request $request * @param null $pathOffset * @param array $options * @return null|RouteMatch */ public function match(Request $request, $pathOffset = null, array $options = array()) { $routeMatch = parent::match($request, $pathOffset, $options); if (!$routeMatch) { return null; } $subject = $routeMatch->getParam($this->identifier); try { $this->getSubjectManager()->findSubjectByString($subject, $this->getInstanceManager()->getInstanceFromRequest()); return $routeMatch; } catch (\Exception $e) { return null; } }
<?php require "./vendor/autoload.php"; $route = \Zend\Mvc\Router\Http\Segment::factory(array('route' => '/:controller', 'constraints' => array('controller' => '[a-zA-Z]+'), 'defaults' => array('param1' => 'val1', 'param2' => 'val2'))); $req = new \Zend\Http\Request(); $req->setUri('http://monsite/stockst'); $match = $route->match($req); if ($match !== null) { echo $match->getParam('param1') . "\n"; } else { echo "ressource non connue \n"; }
/** * Map a route to a callback * * @param string|Router\RouteInterface $route * @param callable $controller * @return Route * @throws Exception\InvalidRouteException */ public function map($route, $controller) { if (is_string($route)) { $route = Router\Http\Segment::factory(array('route' => $route)); } if (!$route instanceof Router\RouteInterface) { throw new Exception\InvalidRouteException('Routes are expected to be either strings or instances of Zend\\Mvc\\Router\\RouteInterface'); } $route = new Route($route, $controller); $this->routes[] = $route; return $route; }
public function testMapCanReceiveARouteObject() { $route = Routes\Segment::factory(array('route' => '/:controller')); $map = $this->app->map($route, function ($params, $app) { }); $this->assertSame($route, $map->route()); }
public function testEncodeCache() { $params1 = array('p1' => 6.123, 'p2' => 7); $uri1 = 'example.com/' . join('/', $params1); $params2 = array('p1' => 6, 'p2' => 'test'); $uri2 = 'example.com/' . join('/', $params2); $route = new Segment('example.com/:p1/:p2'); $request = new Request(); $request->setUri($uri1); $route->match($request); $this->assertSame($uri1, $route->assemble($params1)); $request->setUri($uri2); $route->match($request); $this->assertSame($uri2, $route->assemble($params2)); }
<?php /** * Created by PhpStorm. * User: user * Date: 07/12/15 * Time: 14:43 */ require "./vendor/autoload.php"; $route = \Zend\Mvc\Router\Http\Segment::factory(array('route' => '/:chemin', 'constraints' => array('chemin' => '[a-zA-Z]+'))); $req = new \Zend\Http\Request(); $req->setUri('http://monsite/st9'); $match = $route->match($req); if ($match !== null) { echo "ok\n"; } else { echo "ressource non connue \n"; }
public function testCanPassBooleanValueForThirdArgumentToAllowReusingRouteMatches() { $this->router->addRoute('replace', SegmentRoute::factory(array('route' => '/:controller/:action', 'defaults' => array('controller' => 'ZendTest\\Mvc\\Controller\\TestAsset\\SampleController')))); $routeMatch = new RouteMatch(array('controller' => 'foo')); $routeMatch->setMatchedRouteName('replace'); $this->controller->getEvent()->setRouteMatch($routeMatch); $url = $this->plugin->fromRoute('replace', array('action' => 'bar'), true); $this->assertEquals('/foo/bar', $url); }
private function addRoute($routeName, $route, $routeOptions, $options) { if (isset($options['routeName'])) { $routeName = $options['routeName']; unset($options['routeName']); } if (isset($options['route'])) { $route = $options['route']; unset($options['route']); } $routeOptions = array_merge($routeOptions, $options); $routeSegment = Segment::factory(['route' => $route, 'defaults' => $routeOptions]); $this->router->addRoute($routeName, $routeSegment); }