$ route [ '(ru | en)' ] = $ route [ 'default_controller' ];
$ route [ '(ru | en) / (. +)' ] = "$ 2" ; * This source code was highlighted with Source Code Highlighter .
<? php
if (! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
//
ini_set ( 'include_path' , ini_get ( 'include_path' ) .PATH_SEPARATOR.BASEPATH. 'extensions /' );
require_once ( 'Zend / Loader.php' );
Zend_Loader :: loadClass ( 'Zend_Acl' );
Zend_Loader :: loadClass ( 'Zend_Acl_Role' );
Zend_Loader :: loadClass ( 'Zend_Acl_Resource' );
* This source code was highlighted with Source Code Highlighter .
<? php
function init_roles () {
$ acl = new Zend_Acl ();
// define resources / controllers
$ acl-> add ( new Zend_Acl_Resource ( 'login' ));
$ acl-> add ( new Zend_Acl_Resource ( 'welcome' ));
$ acl-> add ( new Zend_Acl_Resource ( 'logout' ));
// define roles
$ acl-> addRole ( new Zend_Acl_Role ( 'guest' ));
$ acl-> addRole ( new Zend_Acl_Role ( 'member' ));
// determine access
$ acl-> allow ( 'guest' , 'login' );
$ acl-> deny ( 'guest' , 'welcome' );
$ acl-> deny ( 'guest' , 'logout' );
$ acl-> deny ( 'member' , 'login' );
$ acl-> allow ( 'member' , 'welcome' );
$ acl-> allow ( 'member' , 'logout' );
return $ acl;
}
* This source code was highlighted with Source Code Highlighter .
$ autoload [ 'helper' ] = array ( 'zend_framework_loader' , 'zend_framework_acl' );
* This source code was highlighted with Source Code Highlighter .
<? php
class MY_Controller extends Controller {
private $ resource = null ;
private $ priviledge = null ;
// default user role
private $ default_role = 'guest' ;
private $ ci;
private $ acl;
private $ language;
public function MY_Controller () {
parent :: Controller ();
// define language
$ lang = $ this -> uri-> segment (1);
if ($ lang == 'en' ) {
$ this -> language = $ lang;
}
else {
$ this -> language = 'ru' ;
}
// load the desired language
switch ($ lang):
case 'en' :
$ this -> lang-> load ( 'interface' , 'english' );
$ this -> config-> set_item ( 'language' , 'english' );
break ;
case 'ru' :
$ this -> lang-> load ( 'interface' , 'russian' );
$ this -> config-> set_item ( 'language' , 'russian' );
break ;
default :
$ this -> lang-> load ( 'interface' , 'russian' );
$ this -> config-> set_item ( 'language' , 'russian' );
break ;
endswitch;
// call init_roles from zend_framework_alc_helper.php
$ this -> acl = init_roles ();
// call our router to determine the controller and method
$ router = & load_class ( 'Router' );
// set the controller to the class and method privilege
$ this -> resource = $ router-> fetch_class ();
$ this -> priviledge = $ router-> fetch_method ();
// just for convenience not to initialize later in the application
$ this -> ci = & get_instance ();
$ acl = $ this -> getACL ();
// if the user exists, then we work with his data
if ($ this -> session-> userdata ( 'userInfo' )) {
$ identity = $ this -> session-> userdata ( 'userInfo' );
$ this -> setDefaultRole ($ identity [ 'user_role' ]);
}
// if the user is not authorized (guest) tries to request something, then redirect him to the authorization page
if (! $ acl-> isAllowed ($ this -> getDefaultRole (), $ this -> getResource (), $ this -> getPriviledge ()) && $ this -> getDefaultRole () == 'guest' ) {
redirect ( 'login' , 'refresh' );
}
// if the user is authorized and tries to request something where there is no access, then redirect him to the authorization page
elseif (! $ acl-> isAllowed ($ this -> getDefaultRole (), $ this -> getResource (), $ this -> getPriviledge ()) && $ this -> getDefaultRole ()! = 'guest' ) {
redirect ( 'welcome' , 'refresh' );
}
}
// setters and getters
public function getInstance () {
return $ this -> ci;
}
public function getACL () {
return $ this -> acl;
}
public function getDefaultRole () {
return $ this -> default_role;
}
public function getPriviledge () {
return $ this -> priviledge;
}
public function getResource () {
return $ this -> resource;
}
public function setDefaultRole ($ role) {
$ this -> default_role = $ role;
}
public function getLang () {
return $ this -> language;
}
}
* This source code was highlighted with Source Code Highlighter .
class Welcome extends Controller {
public function Welcome () {
parent :: Controller ();
}
}
* This source code was highlighted with Source Code Highlighter .
class Welcome extends MY_Controller {
public function Welcome () {
parent :: MY_Controller ();
}
} * This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/30521/
All Articles