Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zend.navigation.pages.mvc.html

Size:23808
Storage flags:no_autoload,compress/gzip (19%)

Zend\Navigation\Page\Mvc — Zend Framework 2 2.4.2 documentation

Zend\Navigation\Page\Mvc

MVC pages are defined using MVC parameters known from the Zend\Mvc component. An MVC page will use Zend\Mvc\Router\RouteStackInterface internally in the getHref() method to generate hrefs, and the isActive() method will compare the Zend\Mvc\Router\RouteMatch params with the page’s params to determine if the page is active.

Note

Starting in version 2.2.0, if you want to re-use any matched route parameters when generating a link, you can do so via the “useRouteMatch” flag. This is particularly useful when creating segment routes that include the currently selected language or locale as an initial segment, as it ensures the links generated all include the matched value.

MVC page options
Key Type Default Description
action String NULL Action name to use when generating href to the page.
controller String NULL Controller name to use when generating href to the page.
params Array array() User params to use when generating href to the page.
route String NULL Route name to use when generating href to the page.
routeMatch Zend\Mvc\Router\RouteMatch NULL RouteInterface matches used for routing parameters and testing validity.
useRouteMatch Boolean FALSE If true, then getHref method will use the routeMatch parameters to assemble the URI
router Zend\Mvc\Router\RouteStackInterface NULL Router for assembling URLs
query Array array() User query params to use when generating href to page

Note

The URI returned is relative to the baseUrl in Zend\Mvc\Router\Http\TreeRouteStack. In the examples, the baseUrl is ‘/’ for simplicity.

getHref() generates the page URI

This example show that MVC pages use Zend\Mvc\Router\RouteStackInterface internally to generate URIs when calling $page->getHref().

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Create route
$route = Zend\Mvc\Router\Http\Segment::factory(array(
   'route'       => '/[:controller[/:action][/:id]]',
   'constraints' => array(
      'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
      'action'     => '[a-zA-Z][a-zA-Z0-9_-]+',
      'id'         => '[0-9]+',
   ),
   array(
      'controller' => 'Album\Controller\Album',
      'action'     => 'index',
   )
));
$router = new Zend\Mvc\Router\Http\TreeRouteStack();
$router->addRoute('default', $route);

// getHref() returns /album/add
$page = new Zend\Navigation\Page\Mvc(array(
    'action'     => 'add',
    'controller' => 'album',
));

// getHref() returns /album/edit/1337
$page = new Zend\Navigation\Page\Mvc(array(
    'action'     => 'edit',
    'controller' => 'album',
    'params'     => array('id' => 1337),
));

 // getHref() returns /album/1337?format=json
$page = new Zend\Navigation\Page\Mvc(array(
    'action'     => 'edit',
    'controller' => 'album',
    'params'     => array('id' => 1337),
    'query'      => array('format' => 'json'),
));

isActive() determines if page is active

This example show that MVC pages determine whether they are active by using the params found in the route match object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * Dispatched request:
 * - controller: album
 * - action:     index
 */
$page1 = new Zend\Navigation\Page\Mvc(array(
    'action'     => 'index',
    'controller' => 'album',
));

$page2 = new Zend\Navigation\Page\Mvc(array(
    'action'     => 'edit',
    'controller' => 'album',
));

$page1->isActive(); // returns true
$page2->isActive(); // returns false

/**
 * Dispatched request:
 * - controller: album
 * - action:     edit
 * - id:         1337
 */
$page = new Zend\Navigation\Page\Mvc(array(
    'action'     => 'edit',
    'controller' => 'album',
    'params'     => array('id' => 1337),
));

// returns true, because request has the same controller and action
$page->isActive();

/**
 * Dispatched request:
 * - controller: album
 * - action:     edit
 */
$page = new Zend\Navigation\Page\Mvc(array(
    'action'     => 'edit',
    'controller' => 'album',
    'params'     => array('id' => null),
));

// returns false, because page requires the id param to be set in the request
$page->isActive(); // returns false

Using routes

Routes can be used with MVC pages. If a page has a route, this route will be used in getHref() to generate the URL for the page.

Note

Note that when using the route property in a page, you do not need to specify the default params that the route defines (controller, action, etc.).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// the following route is added to the ZF router
$route = Zend\Mvc\Router\Http\Segment::factory(array(
   'route'       => '/a/:id',
   'constraints' => array(
      'id' => '[0-9]+',
   ),
   array(
      'controller' => 'Album\Controller\Album',
      'action'     => 'show',
   )
));
$router = new Zend\Mvc\Router\Http\TreeRouteStack();
$router->addRoute('albumShow', $route);

// a page is created with a 'route' option
$page = new Zend\Navigation\Page\Mvc(array(
    'label'      => 'Show album',
    'route'      => 'albumShow',
    'params'     => array('id' => 42)
));

// returns: /a/42
$page->getHref();

Table Of Contents

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Zend\Navigation\Page\Mvc on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net