/application/core
でMY_controller.php
を(接頭辞が設定ファイルで編集することができます)と呼ばれるファイルを作成します。
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
//Initialization code that affects all controllers
}
}
class Public_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
//Initialization code that affects Public controllers. Probably not much needed because everyone can access public.
}
}
class Admin_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
//Initialization code that affects Admin controllers I.E. redirect and die if not logged in or not an admin
}
}
class Member_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
//Initialization code that affects Member controllers. I.E. redirect and die if not logged in
}
}
を
新しいコントローラを作成するときはいつでも、必要なアクセスを決定します
class Book extends Member_Controller {
//Code that will be executed, no need to check anywhere if the user is logged in.
//The user is guaranteed to be logged in if we are executing code here.
//If you define a __construct() here, remember to call parent::__construct();
}
Book
以外のメンバーコントローラーが必要な場合は、Member_Controller
を拡張することができるので、コードの複製が大幅に削減されます。すべてをチェックする必要はありません。
私はあなたの答えを理解しました。これは本当にDRYに従っており、ユーザーの異なるグループに正しいビジネスルールを適用するのに役立ちます。ありがとう、@ケマル・カーネルはあなたの助けを借りてとてもそうです:) – user826224