Creating custom page typesΒΆ

When extending Zend\Navigation\Page\AbstractPage, there is usually no need to override the constructor or the methods setOptions() or setConfig(). The page constructor takes a single parameter, an Array or a Zend\Config object, which is passed to setOptions() or setConfig() respectively. Those methods will in turn call set() method, which will map options to native or custom properties. If the option internal_id is given, the method will first look for a method named setInternalId(), and pass the option to this method if it exists. If the method does not exist, the option will be set as a custom property of the page, and be accessible via $internalId = $page->internal_id; or $internalId = $page->get('internal_id');.

The most simple custom page

The only thing a custom page class needs to implement is the getHref() method.

1
2
3
4
5
6
7
class My\Simple\Page extends Zend\Navigation\Page\AbstractPage
{
    public function getHref()
    {
        return 'something-completely-different';
    }
}

A custom page with properties

When adding properties to an extended page, there is no need to override/modify setOptions() or setConfig().

 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
class My\Navigation\Page extends Zend\Navigation\Page\AbstractPage
{
    protected $foo;
    protected $fooBar;

    public function setFoo($foo)
    {
        $this->foo = $foo;
    }

    public function getFoo()
    {
        return $this->foo;
    }

    public function setFooBar($fooBar)
    {
        $this->fooBar = $fooBar;
    }

    public function getFooBar()
    {
        return $this->fooBar;
    }

    public function getHref()
    {
        return $this->foo . '/' . $this->fooBar;
    }
}

// can now construct using
$page = new My\Navigation\Page(array(
    'label'   => 'Property names are mapped to setters',
    'foo'     => 'bar',
    'foo_bar' => 'baz'
));

// ...or
$page = Zend\Navigation\Page\AbstractPage::factory(array(
    'type'    => 'My\Navigation\Page',
    'label'   => 'Property names are mapped to setters',
    'foo'     => 'bar',
    'foo_bar' => 'baz'
));

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 Creating custom page types 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.