PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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 637. Finding pages in a container

<?php
$container 
= new Zend_Navigation(array(
    array(
        
'label' => 'Page 1',
        
'uri'   => 'page-1',
        
'foo'   => 'bar',
        
'pages' => array(
            array(
                
'label' => 'Page 1.1',
                
'uri'   => 'page-1.1',
                
'foo'   => 'bar',
            ),
            array(
                
'label' => 'Page 1.2',
                
'uri'   => 'page-1.2',
                
'class' => 'my-class',
            ),
            array(
                
'type'   => 'uri',
                
'label'  => 'Page 1.3',
                
'uri'    => 'page-1.3',
                
'action' => 'about'
            
)
        )
    ),
    array(
        
'label'      => 'Page 2',
        
'id'         => 'page_2_and_3',
        
'class'      => 'my-class',
        
'module'     => 'page2',
        
'controller' => 'index',
        
'action'     => 'page1'
    
),
    array(
        
'label'      => 'Page 3',
        
'id'         => 'page_2_and_3',
        
'module'     => 'page3',
        
'controller' => 'index'
    
)
));

// The 'id' is not required to be unique, but be aware that
// having two pages with the same id will render the same id attribute
// in menus and breadcrumbs.
$found $container->findBy('id''page_2_and_3'); // returns Page 2
$found $container->findOneBy('id''page_2_and_3'); // returns Page 2
$found $container->findBy('id''page_2_and_3'true); // returns Page 2 and Page 3
$found $container->findById('page_2_and_3'); // returns Page 2
$found $container->findOneById('page_2_and_3'); // returns Page 2
$found $container->findAllById('page_2_and_3'); // returns Page 2 and Page 3

// Find all matching CSS class my-class
$found $container->findAllBy('class''my-class'); // returns Page 1.2 and Page 2
$found $container->findAllByClass('my-class');  // returns Page 1.2 and Page 2

// Find first matching CSS class my-class
$found $container->findOneByClass('my-class'); // returns Page 1.2

// Find all matching CSS class non-existant
$found $container->findAllByClass('non-existant'); // returns array()

// Find first matching CSS class non-existant
$found $container->findOneByClass('non-existant'); // returns null

// Find all pages with custom property 'foo' = 'bar'
$found $container->findAllBy('foo''bar'); // returns Page 1 and Page 1.1

// To achieve the same magically, 'foo' must be in lowercase.
// This is because 'foo' is a custom property, and thus the
// property name is not normalized to 'Foo'
$found $container->findAllByfoo('bar');

// Find all with controller = 'index'
$found $container->findAllByController('index'); // returns Page 2 and Page 3

Use regular expressions to find pages:

<?php
$found 
$container->findBy('class','/my/'falsetrue); // returns Page 1.2
$found $container->findOneBy('class''/my/'true); // returns Page 1.2
$found $container->findBy('class''/my/'truetrue); // returns Page 1.2 and Page 2
$found $container->findAllBy('class''/my/'true); // returns Page 1.2 and Page 2
$found $container->findOneByClass('/my/'true); // returns Page 1.2
$found $container->findAllByClass('/my/'true); // returns Page 1.2 and Page 2

Zend Framework