$ zf create project. /
; ViewsFurther in the file “application / Bootstrap.php” I create a new method _initNavigation () (please read the comments in the code):
resources.view.encoding = "UTF-8"
resources.view.basePath = APPLICATION_PATH "/ views"
resources.view.helperPath.Application_View_Helper = APPLICATION_PATH "/ views / helpers"
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
')
/ *
* Initialize the navigator object and pass it to the View
*
* @return Zend_Navigation
* /
public function _initNavigation ( )
{
// Bootstrap View
$ this -> bootstrapView ( ) ;
$ view = $ this -> getResource ( 'view' ) ;
// Simple menu structure (can be rendered in XML)
$ pages = array (
array (
'controller' => 'index' ,
'label' => _ ( 'Home Page' ) ,
) ,
array (
'controller' => 'users' ,
'action' => 'index' ,
// I wrap the text in _ (), then to pull it out with a gettext parser
'label' => _ ( 'Users' ) ,
'pages' => array (
array (
'controller' => 'users' ,
'action' => 'new' ,
'label' => _ ( 'Add user' ) ,
) ,
)
) ,
array (
'controller' => 'users' ,
'action' => 'registration' ,
'label' => _ ( 'Registration' ) ,
) ,
array (
'controller' => 'users' ,
'action' => 'login' ,
'label' => _ ( 'Authorization' ) ,
) ,
array (
'controller' => 'users' ,
'action' => 'logout' ,
'label' => _ ( 'Logout' ) ,
)
) ;
// Create a new container based on our structure
$ container = new Zend_Navigation ( $ pages ) ;
// Pass the container to the View
$ view -> menu = $ container ;
return $ container ;
}
}
$ mkdir application / layoutsI add to the template “application / layouts / scripts / default.phtml” the output of the menu and page content:
$ mkdir application / layouts / scripts
$ touch application / layouts / scripts / default.phtml
<div id = "menu">
<h3>
<? php echo $ this -> translate ( 'Menu' ) ; ?> :
</ h3>
<? php echo $ this -> navigation ( ) -> menu ( $ this -> menu ) ; ?>
</ div>
<div id = "content">
<? php echo $ this -> layout ( ) -> content ; ?>
</ div>
; Layout
resources.layout.layout = "default"
resources.layout.layoutPath = APPLICATION_PATH "/ layouts / scripts"
$ zf create controller users
$ zf create action new --controller-name users
$ zf create action registration --controller-name users
$ zf create action login --controller-name users
$ zf create action logout --controller-name users
<div id = "breadcrumbs">
<h3>
<? php echo $ this -> translate ( 'Bread crumbs' ) ; ?> :
</ h3>
<? php echo $ this -> navigation ( ) -> breadcrumbs ( $ this -> menu ) -> setLinkLast ( true ) ; ?>
</ div>
<? php echo $ this -> navigation ( ) -> sitemap ( $ this -> menu ) ; ?>
<? php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/ **
* Initialize the ACL.
* Create roles and resources. Distribute access rights
*
* @return Zend_Acl
* /
protected function _initAcl ( )
{
$ auth = Zend_Auth :: getInstance ( ) ;
// Define the user role.
// If not authorized - means "guest"
$ role = ( $ auth -> hasIdentity ( ) && ! empty ( $ auth -> getIdentity ( ) -> role ) )
? $ auth -> getIdentity ( ) -> role : 'guest' ;
$ acl = new Zend_Acl ( ) ;
// Create Roles
$ acl -> addRole ( new Zend_Acl_Role ( 'guest' ) )
-> addRole ( new Zend_Acl_Role ( 'member' ) , 'guest' )
-> addRole ( new Zend_Acl_Role ( 'administrator' ) , 'member' ) ;
// Create resources
// I use prefixes for naming resources
// "mvc:" - for pages
$ acl -> add ( new Zend_Acl_Resource ( 'mvc: index' ) )
-> add ( new Zend_Acl_Resource ( 'mvc: error' ) )
-> add ( new Zend_Acl_Resource ( 'mvc: users' ) ) ;
// Let the guests on the "face" and on the error page
$ acl -> allow ( 'guest' , array ( 'mvc: error' , 'mvc: index' ) ) ;
// And also to the login and registration pages
$ acl -> allow ( 'guest' , 'mvc: users' , array ( 'login' , 'registration' ) ) ;
// And already the bummer :)
$ acl -> deny ( 'member' , 'mvc: users' , array ( 'login' , 'registration' ) ) ;
// Well, etc.
$ acl -> allow ( 'member' , 'mvc: users' , array ( 'index' , 'logout' ) ) ;
$ acl -> allow ( 'administrator' , 'mvc: users' , array ( 'new' ) ) ;
// Cling ACLs to Zend_Navigation
Zend_View_Helper_Navigation_HelperAbstract :: setDefaultAcl ( $ acl ) ;
Zend_View_Helper_Navigation_HelperAbstract :: setDefaultRole ( $ role ) ;
return $ acl ;
}
/ *
* Initialize the navigator object and pass it to the View
*
* @return Zend_Navigation
* /
public function _initNavigation ( )
{
$ this -> bootstrapView ( ) ;
$ view = $ this -> getResource ( 'view' ) ;
$ pages = array (
array (
'controller' => 'index' ,
'label' => _ ( 'Home Page' ) ,
) ,
array (
'controller' => 'users' ,
'action' => 'index' ,
// Resource for checking access rights
'resource' => 'mvc: users' ,
// And privilege
'privilege' => 'index' ,
'label' => _ ( 'Users' ) ,
'pages' => array (
array (
'controller' => 'users' ,
'action' => 'new' ,
'resource' => 'mvc: users' ,
'privilege' => 'new' ,
'label' => _ ( 'Add user' ) ,
) ,
)
) ,
array (
'controller' => 'users' ,
'action' => 'registration' ,
'resource' => 'mvc: users' ,
'privilege' => 'registration' ,
'label' => _ ( 'Registration' ) ,
) ,
array (
'controller' => 'users' ,
'action' => 'login' ,
'resource' => 'mvc: users' ,
'privilege' => 'login' ,
'label' => _ ( 'Authorization' ) ,
) ,
array (
'controller' => 'users' ,
'action' => 'logout' ,
'resource' => 'mvc: users' ,
'privilege' => 'logout' ,
'label' => _ ( 'Logout' ) ,
)
) ;
$ container = new Zend_Navigation ( $ pages ) ;
$ view -> menu = $ container ;
return $ container ;
}
}
?>
$ view -> menu = $ container ;not very convenient, because you can accidentally wipe this variable in the controller.
$ view -> navigation ( $ container ) ;
<? php echo $ this -> navigation ( ) -> menu ( ) ; ?>
<? php echo $ this -> navigation ( ) -> breadcrumbs ( ) ; ?>
For most applications, one navigation object and one ACL are built. Therefore, you can put the menu in Zend_Registry :: set ('Zend_Navigation', $ AppNavigation) - the helpers themselves will find and do not need to push and specify in the mailout when specifying the helper.
Source: https://habr.com/ru/post/64115/
All Articles