PHP中CodeIgniter入门教程——第八课 继承公用
如果有需求,需要加个后台,是不是要加个登陆验证呢?
这课解释下如何继承公用控制器。
在core下创建 My_Controller.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * * @author LiZeQiao <674531003@qq.com> * @version 2015-02-03第一版 * 父类验证控制器 */ class My_Controller extends CI_Controller { public function __construct(){ parent::__construct(); //需要排除的控制器和方法 $arr=array( 'home'=>array('login','do_login'), ); $controller=$this->router->fetch_class(); //当前模块名 $action=$this->router->fetch_method(); //当前操作名 //echo $action;exit; //获取当前控制器名和动作名 $con_action=$controller.'/'.$action; /*后台登陆页面跳过 start */ if(array_key_exists($controller,$arr)){ if(in_array($action,$arr[$controller])){ return true; } } /*后台登陆页面跳过 end */ /* 验证用户是否登陆 start */ $this->load->library('session'); //$ci = &get_instance(); $userdata = $this->session->userdata('userData'); $this->load->helper('url'); if (!$userdata) { redirect('admin/home/login'); }else{ return true; } /* 验证用户是否登陆 end */ } } |
后台管理控制器继承My_construct.php文件
1 2 3 4 5 6 7 8 9 10 11 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Bg_construct extends My_Controller { public function index() { $this->load->view('welcome_message'); } } |
访问上述url,跳转到登陆界面。
那么,需要后台验证的页面 都先继承下My_construct.php文件就行。
- PHP中CodeIgniter入门教程——第七课 模型介绍
- PHP中CodeIgniter入门教程——第九课 调用函数