📜 ⬆️ ⬇️

Codeigniter - make your life easier (expanding the base controller)

Today, in almost any application, it is necessary to work with several languages ​​and provide access control.
Some time ago I was in my blog describing how to work with these things in the codeignier, but the blog is closed and some questions are still the same.


First, prepare our application:
expanding routes:
Open routes.php (/system/application/config/routes.php)
add the following lines:
$ route [ '(ru | en)' ] = $ route [ 'default_controller' ];
$ route [ '(ru | en) / (. +)' ] = "$ 2" ; * This source code was highlighted with Source Code Highlighter .

Thus, now we can access any method, any controller, in 3 ways:
www.yourapp / controller / action
www.yourapp / ru / controller / action
www.yourapp / en / controller / action

We also create 2 language files interface_lang.php (/system/application/language/english/interface_lang.php and /system/application/language/russian/interface_lang.php)
The language preparations are finished, now we will prepare our access control.
')
For access control, I use Zend_Acl. Very handy thing.
Download ZendFramework. Create the extensions folder (system / extensions) and transfer the library / Zend folder there
Further we create 2 helpers:
zend_framework_loader_helper.php (/system/application/helpers/zend_framework_loader_helper.php)
<? 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 .

and zend_framework_acl_helper.php (/system/application/helpers/zend_framework_acl_helper.php)
<? 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 .

Here I mean that we have a small application that consists of 3 controllers and we need
create only 2 user groups

Next, open autoload.php (/system/application/config/autoload.php)
and add our helpers
$ autoload [ 'helper' ] = array ( 'zend_framework_loader' , 'zend_framework_acl' );
* This source code was highlighted with Source Code Highlighter .

The preparations are finished now we should start using it all,
that is, to implement the verification of this all in our controllers

There are several ways to extend the functionality of the controller:
1. To register the required actions in the constructor.
2. Do the same in the _remap () method.
3. Extend the controller class and inherit its controllers from it.

The first two methods are good when you need to add unique features to your controller, but if you prescribe things the same for all controllers in this way, the code is duplicated, which is not good.
Therefore, we will consider the third method.

I will assume that the user data (such as his id, name, membership in a group) is stored in the session using the standard mechanics sessions for codeigniter

So, let's begin:

Create the file My_Controller.php (/system/application/libraries/My_Controller.php)
<? 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 .

That's actually all further when creating a new controller, just replace
class Welcome extends Controller {

public function Welcome () {
parent :: Controller ();
}
}
* This source code was highlighted with Source Code Highlighter .

on
class Welcome extends MY_Controller {

public function Welcome () {
parent :: MY_Controller ();
}
} * This source code was highlighted with Source Code Highlighter .

and you can work))

Source: https://habr.com/ru/post/30521/


All Articles