routes.php 内の codeigniter セッション クラスにアクセスするにはどうすればよいですか?
ユーザーが管理者権限 ( $this->session->userdata('logged')
) を持っていない限り、すべての要求 (/login を除く) を特定のコントローラーにルーティングするには、そのクラスが必要です。
すべてのルート ルールが機能しているため、そのクラスにアクセスするだけで済みます。
routes.php 内の codeigniter セッション クラスにアクセスするにはどうすればよいですか?
ユーザーが管理者権限 ( $this->session->userdata('logged')
) を持っていない限り、すべての要求 (/login を除く) を特定のコントローラーにルーティングするには、そのクラスが必要です。
すべてのルート ルールが機能しているため、そのクラスにアクセスするだけで済みます。
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
これを作成して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');
}
}
}
注: 上記のコードは、ユーザー ログイン システムが既に整っていることを前提としています。