登出自动加载类库 有些类库在应用中到处用到。让codeigniter使他们自动加载,不要每次都去这样调用:$this->library->load(‘session’)编辑:system/application/config/autoload.php 找到这行: // ... $autoload['libraries'] = array('session'); // ... 同样自动加载URL Helper 找到这行: // ... $autoload['helper'] = array('url'); // ... Logout controller建立:system/application/controllers/logout.php <?php class Logout extends Controller {
public function index() {
$this->session->sess_destroy();
$this->load->view('logout');
}
} Logout 视图建立:system/application/views/logout.php <!DOCTYPE html> <html lang="en"> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Logout</title>
<link rel="stylesheet" href="<?php echo base_url(); ?>css/style.css" type="text/css" media="all">
<meta http-equiv="refresh" content="3;url=<?php echo base_url(); ?>">
</head> <body> <div>
<p>
You are now logged out.
</p>
<p>
Redirecting you <?php echo anchor('/','home'); ?>.
</p>
</div> </body> </html> Logout页显示简单的信息,并用meta refresh 重定向到首页 Style changes编辑:css/style.css body {
font-family: "Trebuchet MS",Arial;
font-size: 14px;
background-color: #212426;
color: #B9AA81;
} a {
color: #FFF;
} a:hover {
color: #B9AA81;
} input, textarea, select {
font-family:inherit;
font-size:inherit;
font-weight:inherit;
} #signup_form {
margin-left: auto;
margin-right: auto;
width: 360px;
font-size: 16px;
} #signup_form .heading {
text-align: center;
font-size: 22px;
font-weight: bold;
color: #B9AA81;
} #signup_form form {
background-color: #B9AA81;
padding: 10px;
border-radius: 8px;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
} #signup_form form label {
font-weight: bold;
color: #11151E;
} #signup_form form input[type=text],input[type=password] {
width: 316px;
font-weight: bold;
padding: 8px;
border: 1px solid #FFF;
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
} #signup_form form input[type=submit] {
display: block;
margin: auto;
width: 200px;
font-size: 18px;
background-color: #FFF;
border: 1px solid #BBB;
} #signup_form form input[type=submit]:hover {
border-color: #000;
} #signup_form .error {
font-size: 13px;
color: #690C07;
font-style: italic;
} Home 视图现在我们看看,获取登录用户的用户信息是多么容易。 建立:system/application/views/home.php <!DOCTYPE html> <html lang="en"> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Home</title>
<link rel="stylesheet" href="<?php echo base_url(); ?>css/style.css" type="text/css" media="all">
</head> <body> <div>
<?php if(Current_User::user()): ?>
<h2>Hello <em><?php echo Current_User::user()->username; ?></em>.</h2>
<h2><?php echo anchor('logout','Logout'); ?></h2>
<?php else: ?>
<h2>New Users: <?php echo anchor('signup','Create an Account'); ?>.</h2>
<h2>Members: <?php echo anchor('login','Login'); ?>.</h2>
<?php endif; ?>
</div> </body> </html> Home controller编辑: system/application/controllers/home.php <?php class Home extends Controller {
public function index() {
$this->load->view('home');
}
} 测试一下访问:http://localhost/ci_doctrine/logout 确认你是否已经登出。 首先你会看到:
一会后,会回到home页
点击 Login 链接,会来到 登录表单:
输入正确的登录信息,你会再次转到home页,你会看到:
这里我的用户名是burak,它是显示给我看的。 下期教程我们将学习Doctrine的CRUD(建立,读取,更新,删除)等一些功能。 |