Pages | Zend_Navigation Programmer's Reference Guide | Zend_Oauth

# Containers

Containers have methods for adding, retrieving, deleting and iterating pages. Containers implement the » SPL interfaces RecursiveIterator and Countable, meaning that a container can be iterated using the SPL RecursiveIteratorIterator class.

# Creating containers

Zend_Navigation_Container is abstract, and can not be instantiated directly. Use Zend_Navigation if you want to instantiate a container.

Zend_Navigation can be constructed entirely empty, or take an array or a Zend_Config object with pages to put in the container. Each page in the given array/config will eventually be passed to the addPage() method of the container class, which means that each element in the array/config can be an array or a config object, or a Zend_Navigation_Page instance.

Example #1 Creating a container using an array

1. /*
2. * Create a container from an array
3. *
4. * Each element in the array will be passed to
5. * Zend_Navigation_Page::factory() when constructing.
6. */
7. $container = new Zend_Navigation( array (
8. array (
9. 'label' => 'Page 1',
10. 'id' => 'home-link',
11. 'uri' => '/'
12. ),
13. array (
14. 'label' => 'Zend',
15. 'uri' => 'http://www.zend-project.com/',
16. 'order' => 100
17. ),
18. array (
19. 'label' => 'Page 2',
20. 'controller' => 'page2',
21. 'pages' => array (
22. array (
23. 'label' => 'Page 2.1',
24. 'action' => 'page2_1',
25. 'controller' => 'page2',
26. 'class' => 'special-one',
27. 'title' => 'This element has a special class',
28. 'active' => true
29. ),
30. array (
31. 'label' => 'Page 2.2',
32. 'action' => 'page2_2',
33. 'controller' => 'page2',
34. 'class' => 'special-two',
35. 'title' => 'This element has a special class too'
36. )
37. )
38. ),
39. array (
40. 'label' => 'Page 2 with params',
41. 'action' => 'index',
42. 'controller' => 'page2',
43. // specify a param or two
44. 'params' => array (
45. 'format' => 'json',
46. 'foo' => 'bar'
47. )
48. ),
49. array (
50. 'label' => 'Page 2 with params and a route',
51. 'action' => 'index',
52. 'controller' => 'page2',
53. // specify a route name and a param for the route
54. 'route' => 'nav-route-example',
55. 'params' => array (
56. 'format' => 'json'
57. )
58. ),
59. array (
60. 'label' => 'Page 3',
61. 'action' => 'index',
62. 'controller' => 'index',
63. 'module' => 'mymodule',
64. 'reset_params' => false
65. ),
66. array (
67. 'label' => 'Page 4',
68. 'uri' => '#',
69. 'pages' => array (
70. array (
71. 'label' => 'Page 4.1',
72. 'uri' => '/page4',
73. 'title' => 'Page 4 using uri',
74. 'pages' => array (
75. array (
76. 'label' => 'Page 4.1.1',
77. 'title' => 'Page 4 using mvc params',
78. 'action' => 'index',
79. 'controller' => 'page4',
80. // let's say this page is active
81. 'active' => '1'
82. )
83. )
84. )
85. )
86. ),
87. array (
88. 'label' => 'Page 0?',
89. 'uri' => '/setting/the/order/option',
90. // setting order to -1 should make it appear first
91. 'order' => -1
92. ),
93. array (
94. 'label' => 'Page 5',
95. 'uri' => '/',
96. // this page should not be visible
97. 'visible' => false,
98. 'pages' => array (
99. array (
100. 'label' => 'Page 5.1',
101. 'uri' => '#',
102. 'pages' => array (
103. array (
104. 'label' => 'Page 5.1.1',
105. 'uri' => '#',
106. 'pages' => array (
107. array (
108. 'label' => 'Page 5.1.2',
109. 'uri' => '#',
110. // let's say this page is active
111. 'active' => true
112. )
113. )
114. )
115. )
116. )
117. )
118. ),
119. array (
120. 'label' => 'ACL page 1 (guest)',
121. 'uri' => '#acl-guest',
122. 'resource' => 'nav-guest',
123. 'pages' => array (
124. array (
125. 'label' => 'ACL page 1.1 (foo)',
126. 'uri' => '#acl-foo',
127. 'resource' => 'nav-foo'
128. ),
129. array (
130. 'label' => 'ACL page 1.2 (bar)',
131. 'uri' => '#acl-bar',
132. 'resource' => 'nav-bar'
133. ),
134. array (
135. 'label' => 'ACL page 1.3 (baz)',
136. 'uri' => '#acl-baz',
137. 'resource' => 'nav-baz'
138. ),
139. array (
140. 'label' => 'ACL page 1.4 (bat)',
141. 'uri' => '#acl-bat',
142. 'resource' => 'nav-bat'
143. )
144. )
145. ),
146. array (
147. 'label' => 'ACL page 2 (member)',
148. 'uri' => '#acl-member',
149. 'resource' => 'nav-member'
150. ),
151. array (
152. 'label' => 'ACL page 3 (admin',
153. 'uri' => '#acl-admin',
154. 'resource' => 'nav-admin',
155. 'pages' => array (
156. array (
157. 'label' => 'ACL page 3.1 (nothing)',
158. 'uri' => '#acl-nada'
159. )
160. )
161. ),
162. array (
163. 'label' => 'Zend Framework',
164. 'route' => 'zf-route'
165. )
166. ));

Example #2 Creating a container using a config object

Example for INI:

1. [nav]
2. zend.label = "Zend"
3. zend.uri = "http://www.zend-project.com/"
4. zend.order = "100"
6. page1.label = "Page 1"
7. page1.uri = "page1"
8. page1.pages.page1_1.label = "Page 1.1"
9. page1.pages.page1_1.uri = "page1/page1_1"
11. page2.label = "Page 2"
12. page2.uri = "page2"
13. page2.pages.page2_1.label = "Page 2.1"
14. page2.pages.page2_1.uri = "page2/page2_1"
15. page2.pages.page2_2.label = "Page 2.2"
16. page2.pages.page2_2.uri = "page2/page2_2"
17. page2.pages.page2_2.pages.page2_2_1.label = "Page 2.2.1"
18. page2.pages.page2_2.pages.page2_2_1.uri = "page2/page2_2/page2_2_1"
19. page2.pages.page2_2.pages.page2_2_2.label = "Page 2.2.2"
20. page2.pages.page2_2.pages.page2_2_2.uri = "page2/page2_2/page2_2_2"
21. page2.pages.page2_2.pages.page2_2_2.active = "1"
22. page2.pages.page2_3.label = "Page 2.3"
23. page2.pages.page2_3.uri = "page2/page2_3"
24. page2.pages.page2_3.pages.page2_3_1.label = "Page 2.3.1"
25. page2.pages.page2_3.pages.page2_3_1.uri = "page2/page2_3/page2_3_1"
26. page2.pages.page2_3.pages.page2_3_2.label = "Page 2.3.2"
27. page2.pages.page2_3.pages.page2_3_2.uri = "page2/page2_3/page2_3_2"
28. page2.pages.page2_3.pages.page2_3_2.visible = "0"
29. page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_1.label = "Page 2.3.2.1"
30. page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_1.uri = "page2/page2_3/page2_3_2/1"
31. page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_1.active = "1"
32. page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.label = "Page 2.3.2.2"
33. page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.uri = "page2/page2_3/page2_3_2/2"
34. page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.active = "1"
35. page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.pages.page_2_3_2_2_1.label = "Ignore"
36. page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.pages.page_2_3_2_2_1.uri = "#"
37. page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.pages.page_2_3_2_2_1.active = "1"
38. page2.pages.page2_3.pages.page2_3_3.label = "Page 2.3.3"
39. page2.pages.page2_3.pages.page2_3_3.uri = "page2/page2_3/page2_3_3"
40. page2.pages.page2_3.pages.page2_3_3.resource = "admin"
41. page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_1.label = "Page 2.3.3.1"
42. page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_1.uri = "page2/page2_3/page2_3_3/1"
43. page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_1.active = "1"
44. page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_2.label = "Page 2.3.3.2"
45. page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_2.uri = "page2/page2_3/page2_3_3/2"
46. page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_2.resource = "guest"
47. page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_2.active = "1"
49. page3.label = "Page 3"
50. page3.uri = "page3"
51. page3.pages.page3_1.label = "Page 3.1"
52. page3.pages.page3_1.uri = "page3/page3_1"
53. page3.pages.page3_1.resource = "guest"
54. page3.pages.page3_2.label = "Page 3.2"
55. page3.pages.page3_2.uri = "page3/page3_2"
56. page3.pages.page3_2.resource = "member"
57. page3.pages.page3_2.pages.page3_2_1.label = "Page 3.2.1"
58. page3.pages.page3_2.pages.page3_2_1.uri = "page3/page3_2/page3_2_1"
59. page3.pages.page3_2.pages.page3_2_2.label = "Page 3.2.2"
60. page3.pages.page3_2.pages.page3_2_2.uri = "page3/page3_2/page3_2_2"
61. page3.pages.page3_2.pages.page3_2_2.resource = "admin"
62. page3.pages.page3_3.label = "Page 3.3"
63. page3.pages.page3_3.uri = "page3/page3_3"
64. page3.pages.page3_3.resource = "special"
65. page3.pages.page3_3.pages.page3_3_1.label = "Page 3.3.1"
66. page3.pages.page3_3.pages.page3_3_1.uri = "page3/page3_3/page3_3_1"
67. page3.pages.page3_3.pages.page3_3_1.visible = "0"
68. page3.pages.page3_3.pages.page3_3_2.label = "Page 3.3.2"
69. page3.pages.page3_3.pages.page3_3_2.uri = "page3/page3_3/page3_3_2"
70. page3.pages.page3_3.pages.page3_3_2.resource = "admin"
72. home.label = "Home"
73. home.order = "-100"
74. home.module = "default"
75. home.controller = "index"
76. home.action = "index"

1. $config = new Zend_Config_Ini('/path/to/navigation.ini', 'nav');
2. $container = new Zend_Navigation($config);

Example for JSON:

1. {
2. "nav": {
3. "zend": {
4. "label": "Zend",
5. "uri": "http:\/\/www.zend-project.com\/",
6. "order": "100"
7. },
8. "page1": {
9. "label": "Page 1",
10. "uri": "page1",
11. "pages": {
12. "page1_1": {
13. "label": "Page 1.1",
14. "uri": "page1\/page1_1"
15. }
16. }
17. },
18. "page2": {
19. "label": "Page 2",
20. "uri": "page2",
21. "pages": {
22. "page2_1": {
23. "label": "Page 2.1",
24. "uri": "page2\/page2_1"
25. },
26. "page2_2": {
27. "label": "Page 2.2",
28. "uri": "page2\/page2_2",
29. "pages": {
30. "page2_2_1": {
31. "label": "Page 2.2.1",
32. "uri": "page2\/page2_2\/page2_2_1"
33. },
34. "page2_2_2": {
35. "label": "Page 2.2.2",
36. "uri": "page2\/page2_2\/page2_2_2",
37. "active": "1"
38. }
39. }
40. },
41. "page2_3": {
42. "label": "Page 2.3",
43. "uri": "page2\/page2_3",
44. "pages": {
45. "page2_3_1": {
46. "label": "Page 2.3.1",
47. "uri": "page2\/page2_3\/page2_3_1"
48. },
49. "page2_3_2": {
50. "label": "Page 2.3.2",
51. "uri": "page2\/page2_3\/page2_3_2",
52. "visible": "0",
53. "pages": {
54. "page2_3_2_1": {
55. "label": "Page 2.3.2.1",
56. "uri": "page2\/page2_3\/page2_3_2\/1",
57. "active": "1"
58. },
59. "page2_3_2_2": {
60. "label": "Page 2.3.2.2",
61. "uri": "page2\/page2_3\/page2_3_2\/2",
62. "active": "1",
63. "pages": {
64. "page_2_3_2_2_1": {
65. "label": "Ignore",
66. "uri": "#",
67. "active": "1"
68. }
69. }
70. }
71. }
72. },
73. "page2_3_3": {
74. "label": "Page 2.3.3",
75. "uri": "page2\/page2_3\/page2_3_3",
76. "resource": "admin",
77. "pages": {
78. "page2_3_3_1": {
79. "label": "Page 2.3.3.1",
80. "uri": "page2\/page2_3\/page2_3_3\/1",
81. "active": "1"
82. },
83. "page2_3_3_2": {
84. "label": "Page 2.3.3.2",
85. "uri": "page2\/page2_3\/page2_3_3\/2",
86. "resource": "guest",
87. "active": "1"
88. }
89. }
90. }
91. }
92. }
93. }
94. },
95. "page3": {
96. "label": "Page 3",
97. "uri": "page3",
98. "pages": {
99. "page3_1": {
100. "label": "Page 3.1",
101. "uri": "page3\/page3_1",
102. "resource": "guest"
103. },
104. "page3_2": {
105. "label": "Page 3.2",
106. "uri": "page3\/page3_2",
107. "resource": "member",
108. "pages": {
109. "page3_2_1": {
110. "label": "Page 3.2.1",
111. "uri": "page3\/page3_2\/page3_2_1"
112. },
113. "page3_2_2": {
114. "label": "Page 3.2.2",
115. "uri": "page3\/page3_2\/page3_2_2",
116. "resource": "admin"
117. }
118. }
119. },
120. "page3_3": {
121. "label": "Page 3.3",
122. "uri": "page3\/page3_3",
123. "resource": "special",
124. "pages": {
125. "page3_3_1": {
126. "label": "Page 3.3.1",
127. "uri": "page3\/page3_3\/page3_3_1",
128. "visible": "0"
129. },
130. "page3_3_2": {
131. "label": "Page 3.3.2",
132. "uri": "page3\/page3_3\/page3_3_2",
133. "resource": "admin"
134. }
135. }
136. }
137. }
138. },
139. "home": {
140. "label": "Home",
141. "order": "-100",
142. "module": "default",
143. "controller": "index",
144. "action": "index"
145. }
146. }
147. }

1. $config = new Zend_Config_Json('/path/to/navigation.json', 'nav');
2. $container = new Zend_Navigation($config);

Example for XML:

1. <?xml version="1.0" encoding="UTF-8"?>
2. <config>
3. <nav>
5. <zend>
6. <label>Zend</label>
7. <uri>http://www.zend-project.com/</uri>
8. <order>100</order>
9. </zend>
11. <page1>
12. <label>Page 1</label>
13. <uri>page1</uri>
14. <pages>
16. <page1_1>
17. <label>Page 1.1</label>
18. <uri>page1/page1_1</uri>
19. </page1_1>
21. </pages>
22. </page1>
24. <page2>
25. <label>Page 2</label>
26. <uri>page2</uri>
27. <pages>
29. <page2_1>
30. <label>Page 2.1</label>
31. <uri>page2/page2_1</uri>
32. </page2_1>
34. <page2_2>
35. <label>Page 2.2</label>
36. <uri>page2/page2_2</uri>
37. <pages>
39. <page2_2_1>
40. <label>Page 2.2.1</label>
41. <uri>page2/page2_2/page2_2_1</uri>
42. </page2_2_1>
44. <page2_2_2>
45. <label>Page 2.2.2</label>
46. <uri>page2/page2_2/page2_2_2</uri>
47. <active>1</active>
48. </page2_2_2>
50. </pages>
51. </page2_2>
53. <page2_3>
54. <label>Page 2.3</label>
55. <uri>page2/page2_3</uri>
56. <pages>
58. <page2_3_1>
59. <label>Page 2.3.1</label>
60. <uri>page2/page2_3/page2_3_1</uri>
61. </page2_3_1>
63. <page2_3_2>
64. <label>Page 2.3.2</label>
65. <uri>page2/page2_3/page2_3_2</uri>
66. <visible>0</visible>
67. <pages>
69. <page2_3_2_1>
70. <label>Page 2.3.2.1</label>
71. <uri>page2/page2_3/page2_3_2/1</uri>
72. <active>1</active>
73. </page2_3_2_1>
75. <page2_3_2_2>
76. <label>Page 2.3.2.2</label>
77. <uri>page2/page2_3/page2_3_2/2</uri>
78. <active>1</active>
80. <pages>
81. <page_2_3_2_2_1>
82. <label>Ignore</label>
83. <uri>#</uri>
84. <active>1</active>
85. </page_2_3_2_2_1>
86. </pages>
87. </page2_3_2_2>
89. </pages>
90. </page2_3_2>
92. <page2_3_3>
93. <label>Page 2.3.3</label>
94. <uri>page2/page2_3/page2_3_3</uri>
95. <resource>admin</resource>
96. <pages>
98. <page2_3_3_1>
99. <label>Page 2.3.3.1</label>
100. <uri>page2/page2_3/page2_3_3/1</uri>
101. <active>1</active>
102. </page2_3_3_1>
104. <page2_3_3_2>
105. <label>Page 2.3.3.2</label>
106. <uri>page2/page2_3/page2_3_3/2</uri>
107. <resource>guest</resource>
108. <active>1</active>
109. </page2_3_3_2>
111. </pages>
112. </page2_3_3>
114. </pages>
115. </page2_3>
117. </pages>
118. </page2>
120. <page3>
121. <label>Page 3</label>
122. <uri>page3</uri>
123. <pages>
125. <page3_1>
126. <label>Page 3.1</label>
127. <uri>page3/page3_1</uri>
128. <resource>guest</resource>
129. </page3_1>
131. <page3_2>
132. <label>Page 3.2</label>
133. <uri>page3/page3_2</uri>
134. <resource>member</resource>
135. <pages>
137. <page3_2_1>
138. <label>Page 3.2.1</label>
139. <uri>page3/page3_2/page3_2_1</uri>
140. </page3_2_1>
142. <page3_2_2>
143. <label>Page 3.2.2</label>
144. <uri>page3/page3_2/page3_2_2</uri>
145. <resource>admin</resource>
146. </page3_2_2>
148. </pages>
149. </page3_2>
151. <page3_3>
152. <label>Page 3.3</label>
153. <uri>page3/page3_3</uri>
154. <resource>special</resource>
155. <pages>
157. <page3_3_1>
158. <label>Page 3.3.1</label>
159. <uri>page3/page3_3/page3_3_1</uri>
160. <visible>0</visible>
161. </page3_3_1>
163. <page3_3_2>
164. <label>Page 3.3.2</label>
165. <uri>page3/page3_3/page3_3_2</uri>
166. <resource>admin</resource>
167. </page3_3_2>
169. </pages>
170. </page3_3>
172. </pages>
173. </page3>
175. <home>
176. <label>Home</label>
177. <order>-100</order>
178. <module>default</module>
179. <controller>index</controller>
180. <action>index</action>
181. </home>
183. </nav>
184. </config>

1. $config = new Zend_Config_Xml('/path/to/navigation.xml', 'nav');
2. $container = new Zend_Navigation($config);

Example for YAML:

1. nav:
2. zend:
3. label: Zend
4. uri: http://www.zend-project.com/
5. order: 100
6. page1:
7. label: Page 1
8. uri: page1
9. pages:
10. page1_1:
11. label: Page 1.1
12. uri: page1/page1_1
13. page2:
14. label: Page 2
15. uri: page2
16. pages:
17. page2_1:
18. label: Page 2.1
19. uri: page2/page2_1
20. page2_2:
21. label: Page 2.2
22. uri: page2/page2_2
23. pages:
24. page2_2_1:
25. label: Page 2.2.1
26. uri: page2/page2_2/page2_2_1
27. page2_2_2:
28. label: Page 2.2.2
29. uri: page2/page2_2/page2_2_2
30. active: 1
31. page2_3:
32. label: Page 2.3
33. uri: page2/page2_3
34. pages:
35. page2_3_1:
36. label: Page 2.3.1
37. uri: page2/page2_3/page2_3_1
38. page2_3_2:
39. label: Page 2.3.2
40. uri: page2/page2_3/page2_3_2
41. visible: 0
42. pages:
43. page2_3_2_1:
44. label: Page 2.3.2.1
45. uri: page2/page2_3/page2_3_2/1
46. active: 1
47. page2_3_2_2:
48. label: Page 2.3.2.2
49. uri: page2/page2_3/page2_3_2/2
50. active: 1
51. pages:
52. page_2_3_2_2_1:
53. label: Ignore
54. uri: #
55. active: 1
56. page2_3_3:
57. label: Page 2.3.3
58. uri: page2/page2_3/page2_3_3
59. resource: admin
60. pages:
61. page2_3_3_1:
62. label: Page 2.3.3.1
63. uri: page2/page2_3/page2_3_3/1
64. active: 1
65. page2_3_3_2:
66. label: Page 2.3.3.2
67. uri: page2/page2_3/page2_3_3/2
68. resource: guest
69. active: 1
70. page3:
71. label: Page 3
72. uri: page3
73. pages:
74. page3_1:
75. label: Page 3.1
76. uri: page3/page3_1
77. resource: guest
78. page3_2:
79. label: Page 3.2
80. uri: page3/page3_2
81. resource: member
82. pages:
83. page3_2_1:
84. label: Page 3.2.1
85. uri: page3/page3_2/page3_2_1
86. page3_2_2:
87. label: Page 3.2.2
88. uri: page3/page3_2/page3_2_2
89. resource: admin
90. page3_3:
91. label: Page 3.3
92. uri: page3/page3_3
93. resource: special
94. pages:
95. page3_3_1:
96. label: Page 3.3.1
97. uri: page3/page3_3/page3_3_1
98. visible: 0
99. page3_3_2:
100. label: Page 3.3.2
101. uri: page3/page3_3/page3_3_2
102. resource: admin
103. home:
104. label: Home
105. order: -100
106. module: default
107. controller: index
108. action: index

1. $config = new Zend_Config_Yaml'/path/to/navigation.yaml', 'nav');
2. $container = new Zend_Navigation($config);

# Adding pages

Adding pages to a container can be done with the methods addPage(), addPages(), or setPages(). See examples below for explanation.

Example #3 Adding pages to a container

1. // create container
2. $container = new Zend_Navigation();
4. // add page by giving a page instance
5. $container->addPage(Zend_Navigation_Page::factory( array (
6. 'uri' => 'http://www.example.com/',
7. )));
9. // add page by giving an array
10. $container->addPage( array (
11. 'uri' => 'http://www.example.com/',
12. ));
14. // add page by giving a config object
15. $container->addPage(new Zend_Config( array (
16. 'uri' => 'http://www.example.com/',
17. )));
19. $pages = array (
20. array (
21. 'label' => 'Save',
22. 'action' => 'save',
23. ),
24. array (
25. 'label' => 'Delete',
26. 'action' => 'delete',
27. ),
28. );
30. // add two pages
31. $container->addPages($pages);
33. // add container
34. $container->addPages(new Zend_Navigation( array (
35. array (
36. 'label' => 'Move up',
37. 'action' => 'up',
38. ),
39. array (
40. 'label' => 'Move down',
41. 'action' => 'down',
42. ),
43. )));
45. // remove existing pages and add the given pages
46. $container->setPages($pages);

# Removing pages

Removing pages can be done with removePage() or removePages(). The first method accepts a an instance of a page, or an integer. The integer corresponds to the order a page has. The latter method will remove all pages in the container.

Example #4 Removing pages from a container

1. $container = new Zend_Navigation( array (
2. array (
3. 'label' => 'Page 1',
4. 'action' => 'page1'
5. ),
6. array (
7. 'label' => 'Page 2',
8. 'action' => 'page2',
9. 'order' => 200
10. ),
11. array (
12. 'label' => 'Page 3',
13. 'action' => 'page3'
14. )
15. ));
17. // remove page by implicit page order
18. $container->removePage(0); // removes Page 1
20. // remove page by instance
21. $page3 = $container->findOneByAction('page3');
22. $container->removePage($page3); // removes Page 3
24. // remove page by explicit page order
25. $container->removePage(200); // removes Page 2
27. // remove all pages
28. $container->removePages(); // removes all pages

# Finding pages

Containers have finder methods for retrieving pages. They are findOneBy($property, $value, $useRegex = false), findAllBy($property, $value, $useRegex = false), and findBy($property, $value, $all = false, $useRegex = false). Those methods will recursively search the container for pages matching the given $page->$property == $value or when regular expressions are used: preg_match($value, $page->$property). The first method, findOneBy(), will return a single page matching the property with the given value, or `NULL` if it cannot be found. The second method will return all pages with a property matching the given value. The third method will call one of the two former methods depending on the $all flag.

The finder methods can also be used magically by appending the property name to findBy, findOneBy, or findAllBy, e.g. findOneByLabel('Home') to return the first matching page with label 'Home'. Other combinations are findByLabel(...), findOnyByTitle(...), findAllByController(...), etc. Finder methods also work on custom properties, such as findByFoo('bar').

Example #5 Finding pages in a container

1. $container = new Zend_Navigation( array (
2. array (
3. 'label' => 'Page 1',
4. 'uri' => 'page-1',
5. 'foo' => 'bar',
6. 'pages' => array (
7. array (
8. 'label' => 'Page 1.1',
9. 'uri' => 'page-1.1',
10. 'foo' => 'bar',
11. ),
12. array (
13. 'label' => 'Page 1.2',
14. 'uri' => 'page-1.2',
15. 'class' => 'my-class',
16. ),
17. array (
18. 'type' => 'uri',
19. 'label' => 'Page 1.3',
20. 'uri' => 'page-1.3',
21. 'action' => 'about'
22. )
23. )
24. ),
25. array (
26. 'label' => 'Page 2',
27. 'id' => 'page_2_and_3',
28. 'class' => 'my-class',
29. 'module' => 'page2',
30. 'controller' => 'index',
31. 'action' => 'page1'
32. ),
33. array (
34. 'label' => 'Page 3',
35. 'id' => 'page_2_and_3',
36. 'module' => 'page3',
37. 'controller' => 'index'
38. )
39. ));
41. // The 'id' is not required to be unique, but be aware that
42. // having two pages with the same id will render the same id attribute
43. // in menus and breadcrumbs.
44. $found = $container->findBy('id', 'page_2_and_3'); // returns Page 2
45. $found = $container->findOneBy('id', 'page_2_and_3'); // returns Page 2
46. $found = $container->findBy('id', 'page_2_and_3', true); // returns Page 2 and Page 3
47. $found = $container->findById('page_2_and_3'); // returns Page 2
48. $found = $container->findOneById('page_2_and_3'); // returns Page 2
49. $found = $container->findAllById('page_2_and_3'); // returns Page 2 and Page 3
51. // Find all matching CSS class my-class
52. $found = $container->findAllBy('class', 'my-class'); // returns Page 1.2 and Page 2
53. $found = $container->findAllByClass('my-class'); // returns Page 1.2 and Page 2
55. // Find first matching CSS class my-class
56. $found = $container->findOneByClass('my-class'); // returns Page 1.2
58. // Find all matching CSS class non-existant
59. $found = $container->findAllByClass('non-existant'); // returns array()
61. // Find first matching CSS class non-existant
62. $found = $container->findOneByClass('non-existant'); // returns null
64. // Find all pages with custom property 'foo' = 'bar'
65. $found = $container->findAllBy('foo', 'bar'); // returns Page 1 and Page 1.1
67. // To achieve the same magically, 'foo' must be in lowercase.
68. // This is because 'foo' is a custom property, and thus the
69. // property name is not normalized to 'Foo'
70. $found = $container->findAllByfoo('bar');
72. // Find all with controller = 'index'
73. $found = $container->findAllByController('index'); // returns Page 2 and Page 3

Use regular expressions to find pages:

1. $found = $container->findBy('class','/my/', false, true); // returns Page 1.2
2. $found = $container->findOneBy('class', '/my/', true); // returns Page 1.2
3. $found = $container->findBy('class', '/my/', true, true); // returns Page 1.2 and Page 2
4. $found = $container->findAllBy('class', '/my/', true); // returns Page 1.2 and Page 2
5. $found = $container->findOneByClass('/my/', true); // returns Page 1.2
6. $found = $container->findAllByClass('/my/', true); // returns Page 1.2 and Page 2

# Iterating containers

Zend_Navigation_Container implements RecursiveIteratorIterator, and can be iterated using any Iterator class. To iterate a container recursively, use the RecursiveIteratorIterator class.

Example #6 Iterating a container

1. /*
2. * Create a container from an array
3. */
4. $container = new Zend_Navigation( array (
5. array (
6. 'label' => 'Page 1',
7. 'uri' => '#'
8. ),
9. array (
10. 'label' => 'Page 2',
11. 'uri' => '#',
12. 'pages' => array (
13. array (
14. 'label' => 'Page 2.1',
15. 'uri' => '#'
16. ),
17. array (
18. 'label' => 'Page 2.2',
19. 'uri' => '#'
20. )
21. )
22. ),
23. array (
24. 'label' => 'Page 3',
25. 'uri' => '#'
26. )
27. ));
29. // Iterate flat using regular foreach:
30. // Output: Page 1, Page 2, Page 3
31. foreach ($container as $page) {
32. echo $page->label;
33. }
35. // Iterate recursively using RecursiveIteratorIterator
36. $it = new RecursiveIteratorIterator(
37. $container, RecursiveIteratorIterator::SELF_FIRST);
39. // Output: Page 1, Page 2, Page 2.1, Page 2.2, Page 3
40. foreach ($it as $page) {
41. echo $page->label;
42. }

# Other operations

The method hasPage(Zend_Navigation_Page $page) checks if the container has the given page. The method hasPages() checks if there are any pages in the container, and is equivalent to count($container) > 1.

The toArray() method converts the container and the pages in it to an array. This can be useful for serializing and debugging.

Example #7 Converting a container to an array

1. $container = new Zend_Navigation( array (
2. array (
3. 'label' => 'Page 1',
4. 'uri' => '#'
5. ),
6. array (
7. 'label' => 'Page 2',
8. 'uri' => '#',
9. 'pages' => array (
10. array (
11. 'label' => 'Page 2.1',
12. 'uri' => '#'
13. ),
14. array (
15. 'label' => 'Page 2.2',
16. 'uri' => '#'
17. )
18. )
19. )
20. ));
22. var_dump ($container->toArray());
24. /* Output:
25. array(2) {
26. [0]=> array(15) {
27. ["label"]=> string(6) "Page 1"
28. ["id"]=> NULL
29. ["class"]=> NULL
30. ["title"]=> NULL
31. ["target"]=> NULL
32. ["rel"]=> array(0) {
33. }
34. ["rev"]=> array(0) {
35. }
36. ["order"]=> NULL
37. ["resource"]=> NULL
38. ["privilege"]=> NULL
39. ["active"]=> bool(false)
40. ["visible"]=> bool(true)
41. ["type"]=> string(23) "Zend_Navigation_Page_Uri"
42. ["pages"]=> array(0) {
43. }
44. ["uri"]=> string(1) "#"
45. }
46. [1]=> array(15) {
47. ["label"]=> string(6) "Page 2"
48. ["id"]=> NULL
49. ["class"]=> NULL
50. ["title"]=> NULL
51. ["target"]=> NULL
52. ["rel"]=> array(0) {
53. }
54. ["rev"]=> array(0) {
55. }
56. ["order"]=> NULL
57. ["resource"]=> NULL
58. ["privilege"]=> NULL
59. ["active"]=> bool(false)
60. ["visible"]=> bool(true)
61. ["type"]=> string(23) "Zend_Navigation_Page_Uri"
62. ["pages"]=> array(2) {
63. [0]=> array(15) {
64. ["label"]=> string(8) "Page 2.1"
65. ["id"]=> NULL
66. ["class"]=> NULL
67. ["title"]=> NULL
68. ["target"]=> NULL
69. ["rel"]=> array(0) {
70. }
71. ["rev"]=> array(0) {
72. }
73. ["order"]=> NULL
74. ["resource"]=> NULL
75. ["privilege"]=> NULL
76. ["active"]=> bool(false)
77. ["visible"]=> bool(true)
78. ["type"]=> string(23) "Zend_Navigation_Page_Uri"
79. ["pages"]=> array(0) {
80. }
81. ["uri"]=> string(1) "#"
82. }
83. [1]=>
84. array(15) {
85. ["label"]=> string(8) "Page 2.2"
86. ["id"]=> NULL
87. ["class"]=> NULL
88. ["title"]=> NULL
89. ["target"]=> NULL
90. ["rel"]=> array(0) {
91. }
92. ["rev"]=> array(0) {
93. }
94. ["order"]=> NULL
95. ["resource"]=> NULL
96. ["privilege"]=> NULL
97. ["active"]=> bool(false)
98. ["visible"]=> bool(true)
99. ["type"]=> string(23) "Zend_Navigation_Page_Uri"
100. ["pages"]=> array(0) {
101. }
102. ["uri"]=> string(1) "#"
103. }
104. }
105. ["uri"]=> string(1) "#"
106. }
107. }
108. */

Pages | Zend_Navigation Programmer's Reference Guide | Zend_Oauth