1

routes.php 内の codeigniter セッション クラスにアクセスするにはどうすればよいですか?

ユーザーが管理者権限 ( $this->session->userdata('logged')) を持っていない限り、すべての要求 (/login を除く) を特定のコントローラーにルーティングするには、そのクラスが必要です。

すべてのルート ルールが機能しているため、そのクラスにアクセスするだけで済みます。

4

2 に答える 2

0

You are not typically able to access the Singleton ($this->) from the config & routes file because the classes are not loaded at that point

although there are a few workarounds to access the session, the better way would be to a MY_Controller & the _remap() function:

http://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping

Here's some sample code that explains a little more about how they work:

http://www.codebyjeff.com/blog/2012/11/ci-_remap-function-the-friend-you-never-knew-you-had

于 2013-02-11T12:27:41.980 に答える
0

これを作成してMY_Controllerに保存しapplication/core/、他のコントローラに拡張させます。

<?php if (! defined('BASEPATH')) exit('No direct script access');

class MY_Controller extends CI_Controller {

function __construct() {
    parent::__construct();
    $this->_check_auth();
}

private function _check_auth(){
    if(!$this->session->userdata('is_admin')){
        $this->session->sess_destroy();
        redirect('login');
    }
}
}

注: 上記のコードは、ユーザー ログイン システムが既に整っていることを前提としています。

于 2013-02-11T13:02:13.917 に答える