Programmer's Reference Guide
| 導入 |
画面
Zend_Navigationは2種類の画面タイプを入手可能にします。
MVC画面はオンサイトなweb画面にリンクしていて、 MVCパラメータ(action, controller,
module, route, params)を使って定義されます。
URI画面は単一な属性のuriで定義されます。
そのURI画面はオフサイトな画面をリンクしたり、
生成されたリンク(例えば、<a href="#">foo<a>という形になるURI)
で画面をリンクしたりできる柔軟性を完全に備えています。
Common page features
All page classes must extend Zend_Navigation_Page, and will thus share a common set of features and properties. Most notably they share the options in the table below and the same initialization process.
Option keys are mapped to set methods. This means that
the option order maps to the method setOrder(),
and reset_params maps to the method
setResetParams(). If there is no setter method for
the option, it will be set as a custom property of the page.
Read more on extending Zend_Navigation_Page in Creating custom page types.
| Key | Type | Default | Description |
|---|---|---|---|
label |
String | NULL | A page label, such as 'Home' or 'Blog'. |
id |
String | int |
NULL | An id tag/attribute that may be used when rendering the page, typically in an anchor element. |
class |
String | NULL | A CSS class that may be used when rendering the page, typically in an anchor element. |
title |
String | NULL |
A short page description, typically for using
as the title attribute in an anchor.
|
target |
String | NULL | Specifies a target that may be used for the page, typically in an anchor element. |
rel |
Array | array() |
Specifies forward relations for the page.
Each element in the array is a key-value pair, where the
key designates the relation/link type, and the value is
a pointer to the linked page. An example of a key-value
pair is 'alternate' => 'format/plain.html'.
To allow full flexbility, there are no restrictions on
relation values. The value does not have to be a string.
Read more about rel and rev in
the
section on the Links helper..
|
rev |
Array | array() |
Specifies reverse relations for the page. Works exactly
like rel.
|
order |
String | int | NULL
|
NULL |
Works like order for elements in
Zend_Form. If specified,
the page will be iterated in a specific order, meaning
you can force a page to be iterated before others by
setting the order attribute to a low number,
e.g. -100. If a String is given, it must
parse to a valid int. If NULL
is given, it will be reset, meaning the order in which
the page was added to the container will be used.
|
resource |
String | Zend_Acl_Resource_Interface | NULL | NULL | ACL resource to associate with the page. Read more in the section on ACL integration in view helpers.. |
privilege |
String | NULL | NULL | ACL privilege to associate with the page. Read more in the section on ACL integration in view helpers.. |
active |
bool |
FALSE |
Whether the page should be considered active for the
current request. If active is FALSE or not
given, MVC pages will check its properties against the
request object upon calling $page->isActive().
|
visible |
bool |
TRUE | Whether page should be visible for the user, or just be a part of the structure. Invisible pages are skipped by view helpers. |
pages |
Array | Zend_Config | NULL | NULL | Child pages of the page. This could be an Array or Zend_Config object containing either page options that can be passed to the factory() method, or actual Zend_Navigation_Page instances, or a mixture of both. |
注意: Custom properties
All pages support setting and getting of custom properties by use of the magic methods __set($name, $value), __get($name), __isset($name) and __unset($name). Custom properties may have any value, and will be included in the array that is returned from$page->toArray(), which means that pages can be serialized/deserialized successfully even if the pages contains properties that are not native in the page class.
Both native and custom properties can be set using$page->set($name, $value)and retrieved using$page->get($name), or by using magic methods.
例1 Custom page properties
This example shows how custom properties can be used.
- $page = new Zend_Navigation_Page_Mvc();
- $page->foo = 'bar';
- $page->meaning = 42;
- if ($page->meaning != 42) {
- // action should be taken
- }
Zend_Navigation_Page_Mvc
MVC pages are defined using MVC parameters known from the Zend_Controller component. An MVC page will use Zend_Controller_Action_Helper_Url internally in the getHref() method to generate hrefs, and the isActive() method will intersect the Zend_Controller_Request_Abstract params with the page's params to determine if the page is active.
| 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. |
module |
String | NULL | Module 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. |
reset_params |
bool |
TRUE | Whether user params should be reset when generating href to the page. |
注意: The three examples below assume a default MVC setup with the
defaultroute in place.
The URI returned is relative to thebaseUrlin Zend_Controller_Front. In the examples, the baseUrl is '/' for simplicity.
例2 getHref() generates the page URI
This example shows that MVC pages use
Zend_Controller_Action_Helper_Url internally
to generate URIs when calling $page->getHref().
- // getHref() returns /
- 'action' => 'index',
- 'controller' => 'index'
- ));
- // getHref() returns /blog/post/view
- 'action' => 'view',
- 'controller' => 'post',
- 'module' => 'blog'
- ));
- // getHref() returns /blog/post/view/id/1337
- 'action' => 'view',
- 'controller' => 'post',
- 'module' => 'blog',
- ));
例3 isActive() determines if page is active
This example shows that MVC pages determine whether they are active by using the params found in the request object.
- /*
- * Dispatched request:
- * - module: default
- * - controller: index
- * - action: index
- */
- 'action' => 'index',
- 'controller' => 'index'
- ));
- 'action' => 'bar',
- 'controller' => 'index'
- ));
- $page1->isActive(); // returns true
- $page2->isActive(); // returns false
- /*
- * Dispatched request:
- * - module: blog
- * - controller: post
- * - action: view
- * - id: 1337
- */
- 'action' => 'view',
- 'controller' => 'post',
- 'module' => 'blog'
- ));
- // returns true, because request has the same module, controller and action
- $page->isActive();
- /*
- * Dispatched request:
- * - module: blog
- * - controller: post
- * - action: view
- */
- 'action' => 'view',
- 'controller' => 'post',
- 'module' => 'blog',
- ));
- // returns false, because page requires the id param to be set in the request
- $page->isActive(); // returns false
例4 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 that when using the
routeproperty in a page, you should also specify the default params that the route defines (module, controller, action, etc.), otherwise the isActive() method will not be able to determine if the page is active. The reason for this is that there is currently no way to get the default params from a Zend_Controller_Router_Route_Interface object, nor to retrieve the current route from a Zend_Controller_Router_Interface object.
- // the following route is added to the ZF router
- Zend_Controller_Front::getInstance()->getRouter()->addRoute(
- 'article_view', // route name
- new Zend_Controller_Router_Route(
- 'a/:id',
- 'module' => 'news',
- 'controller' => 'article',
- 'action' => 'view',
- 'id' => null
- )
- )
- );
- // a page is created with a 'route' option
- 'label' => 'A news article',
- 'route' => 'article_view',
- 'module' => 'news', // required for isActive(), see note above
- 'controller' => 'article', // required for isActive(), see note above
- 'action' => 'view', // required for isActive(), see note above
- ));
- // returns: /a/42
- $page->getHref();
Zend_Navigation_Page_Uri(日本語)
Zend_Navigation_Page_Uriタイプの画面は、
他のドメインやサイトの画面にリンクするためや、、
画面にカスタムロジックを実装したりするために使われることができます。
URI画面は単純です;
画面の共通オプションに加えて、URI画面にはたった一つのオプション、
uriがあります。
$page->getHref()を呼び出すときにuriが戻されます。
それは String かまたはNULLです。
注意: Zend_Navigation_Page_Uriは
$page->isActive()を呼び出したときに活動状態かどうか決定しようとはしません。 ただ単に現在設定されているものを返すだけで、 URI画面を活動状態にするには、 手動で$page->setActive()を呼び出すか、 または生成時に、画面オプションにactiveを指定しなければいけません。
| キー | タイプ | 既定値 | 説明 |
|---|---|---|---|
uri |
String | NULL | 画面へのURI。さまざまな文字列または NULL になりうる。 |
カスタム・ページ・タイプの作成
Zend_Navigation_Pageを拡張するとき、 通常は、コンストラクタ、メソッド setOptions()、 または setConfig()をオーバーライドする必要はありません。 ページ・コンストラクタは単一のパラメータ、 Array 、 またはZend_Configオブジェクトを受け取ります。 そして、それはそれぞれ setOptions() または setConfig()に渡されます。 それらのメソッドは次に set()メソッドを呼びます。 そして、オプションをネイティブまたはカスタムのプロパティにマップします。 もし、オプションinternal_idが与えられたら、 メソッドは setInternalId()というメソッドを最初に探して、 それが存在するならばこのメソッドにオプションを渡します。 メソッドが存在しなければ、オプションはページのカスタム・プロパティとしてセットされて、 $internalId = $page->internal_id;または $internalId = $page->get('internal_id');を通じてアクセスできます。
例5 もっとも単純なカスタム・ページ
カスタム・ページ・クラスで実装する必要がある唯一のものは、 getHref()メソッドです。
- class My_Simple_Page extends Zend_Navigation_Page
- {
- public function getHref()
- {
- return 'something-completely-different';
- }
- }
例6 プロパティ付のカスタム・ページ
拡張したページにプロパティを追加するとき、 setOptions()や setConfig() メソッドをオーバーライドしたり、修正する必要はありません。
- class My_Navigation_Page extends Zend_Navigation_Page
- {
- private $_foo;
- private $_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;
- }
- }
- //これで、利用して構築できます
- 'label' => 'Property names are mapped to setters',
- 'foo' => 'bar',
- 'foo_bar' => 'baz'
- ));
- //または
- 'type' => 'My_Navigation_Page',
- 'label' => 'Property names are mapped to setters',
- 'foo' => 'bar',
- 'foo_bar' => 'baz'
- ));
ページ・ファクトリを使ってページを作成
すべてのページ(また、カスタマイズしたクラス)を、
ページ・ファクトリ Zend_Navigation_Page::factory() を用いて
作成できます。
ファクトリは任意の配列、
またはZend_Configオブジェクトをとることができます。
ページの節でご覧いただけるように、
配列または構成の各々のキーはページ・オプションと一致します。
uriが与えられ、MVCオプション
(action, controller, module, route)
が与えられないなら、
URIページが作成されます。
MVCオプションのいずれかが与えられると、
MVCページが作成されます。
typeが与えられると、
ファクトリは、その値が作成されるべきであるクラスの名前であると仮定します。
もし、値が mvc または uri ならば、
MVC/URI 画面が作成されます。
例7 ページ・ファクトリを使ってMVCページを作成
- 'label' => 'My MVC page',
- 'action' => 'index'
- ));
- 'label' => 'Search blog',
- 'action' => 'index',
- 'controller' => 'search',
- 'module' => 'blog'
- ));
- 'label' => 'Home',
- 'action' => 'index',
- 'controller' => 'index',
- 'module' => 'index',
- 'route' => 'home'
- ));
- 'type' => 'mvc',
- 'label' => 'My MVC page'
- ));
例8 ページ・ファクトリを使ってURIページを作成
- 'label' => 'My URI page',
- 'uri' => 'http://www.example.com/'
- ));
- 'label' => 'Search',
- 'uri' => 'http://www.example.com/search',
- 'active' => true
- ));
- 'label' => 'My URI page',
- 'uri' => '#'
- ));
- 'type' => 'uri',
- 'label' => 'My URI page'
- ));
例9 ページ・ファクトリを使ってカスタムページ型を作成
ページ・ファクトリを使ってカスタムページ型を作成するには、
インスタンス化するクラス名を指定するために、
typeオプションを使ってください。
- class My_Navigation_Page extends Zend_Navigation_Page
- {
- protected $_fooBar = 'ok';
- public function setFooBar($fooBar)
- {
- $this->_fooBar = $fooBar;
- }
- }
- 'type' => 'My_Navigation_Page',
- 'label' => 'My custom page',
- 'foo_bar' => 'foo bar'
- ));
| 導入 |
+ Add A Comment
Please do not report issues via comments; use the ZF Issue Tracker.
If you have a JIRA/Crowd account, we suggest you login first before commenting.

Comments
If your application uses a mix of routes, it's always a good idea to specify exactly which route to use for each navigation page so that the URLs are created correctly.
If your application uses a mix of routes, it's always a good idea to specify exactly which route to use for each navigation page so that the URLs are created correctly.
Copy and paste this (1.11)
/application/configs/navigation.xml
--------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<nav>
<home>
<label>Home</label>
<controller>index</controller>
<action>index</action>
</home>
<user>
<label>Users</label>
<controller>user</controller>
<action>index</action>
<pages>
<login>
<label>Login</label>
<controller>index</controller>
<action>index</action>
</login>
<register>
<label>Register</label>
<controller>register</controller>
<action>index</action>
</register>
</pages>
</user>
</nav>
</configdata>
/application/Bootstrap.php
-------------------------------------------------
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initNavigation()
{
$this->bootstrap("layout");
$layout = $this->getResource('layout');
$view = $layout->getView();
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
$navigation = new Zend_Navigation($config);
$view->navigation($navigation);
}
}
application/layouts/scripts/layout.phtml
---------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<?php echo $this->headTitle() ?>
<?php echo $this->headMeta() ?>
<?php echo $this->headScript() ?>
<?php echo $this->headStyle() ?>
</head>
<body>
<?php echo $this->navigation()->menu()->setMaxDepth(1); ?>
<?php echo $this->layout()->content; ?>
</body>
</html>
Bing, bang, boom. This is ALL anyone wanted to come here for.
Instead of getting the view in the bootstrap in Dominic's example. You can put the navigation container into the registry.
$navigation = new Zend_Navigation($config);
Zend_Registry::set('Zend_Navigation', $navigation);
Then calling when doing:
$this->navigation()->menu()
In the view container stored in the registry will be used.