100分求解:cakephp的controllers问题
为什么controllers里的函数不能被调用啊,郁闷,有没有用过的大虾指导一下。本人刚学这个。十分感谢!在线等。
具体情况是:
首先,访问http://localhost,显示login界面
然后,在login界面输入用户名密码提交Form,出现以下错误:
无法显示网页,可能是网络问题或404错误!
您可以尝试刷新此链接:
http://localhost/clients/auth
没有道理的啊,是不是设置有问题?按照cakephp文档上讲应该是可以访问/clients/auth的。
具体的可以参考:http://book.cakephp.org/cn/view/977/Controller-Methods
1. C:\Apache2\htdocs\app\config\routes.php
Router::connect('/', array('controller' => 'clients', 'action' => 'login'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
2. C:\Apache2\htdocs\app\views\clients\login.ctp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form action="/clients/auth" method="post">
<table>
<tr>
<td><label>用户名:</label><input type="text" name="data[username]"/></td>
</tr>
<tr>
<td><label>密码:</label><input type="password" name="data[passwd]"/></td>
</tr>
<tr>
<td> </td>
<td><button type="submit">登 录</button></td>
</tr>
</table>
</form>
</body>
</html>
3. C:\Apache2\htdocs\app\controllers\clients_controller.php
<?php
class ClientsController extends AppController {
var $name = 'Clients';
var $uses = array();
function index() {
$this->render('/clients/login');
}
function login() {
$this->render('/clients/login');
}
function logout() {
$this->Session->destory();
}
function auth() {
$username = $this->data['username'];
$passwd = $this->data['passwd'];
$this->loadModel('Client');
$client = $this->Client->find('first', array('conditions' => array('username'=>$username)));
if(empty($client['Client']['uid'])) {
$this->render('/clients/login');
} else {
$this->loadModel('User');
$user = $this->User->find('first', array('conditions' => array('uid'=>$client['Client']['uid'], 'passwd'=>md5($passwd))));
// 如果无法找到该用户,则跳转至
if(empty($user)) $this->render('/clients/login');
else {
$full_name = $user['User']['fname']." ".$user['User']['lname'];
$this->Session->write('uid',$user['User']['uid']);
$this->Session->write('mid',$client['Client']['mid']);
$this->Session->write('email',$user['User']['email']);
$this->Session->write('nickname',$full_name);
$this->redirect(array('controller' => 'books', 'action' => 'pending'));
}
}
}
function beforeFilter() {
echo "test beforeFilter";
$this->auth;
}
function beforeRender() {
echo "test beforeRender";
$this->auth;
}
function afterFilter() {
echo "test afterFilter";
$this->auth;
}
}
?>
有愿意讨论的欢迎加我QQ:30490031.