我是 cakephp 新手...我正在使用 CakePhp 和 Mysql 开发一个应用程序。在 Cakephp 中,我有两个表,一个是表单(id、名称、创建、修改)和属性(id、属性名称、form_id、值),我只将所有内容保存在表单控制器中的一个控制器中。在这个 formsctrller 中,我具有登录、注册、查看表单等操作...
Do i need to create a separate controller for login and register..If so Actually i will run in my system with http://localhost/cake/forms where i am having the place to register and login ...If i had separate controller,then how can i make that to switch from forms to login controller on login....Explain me...
编辑
<?php
class UsersController extends AppController
{
var $name = 'Users';
var $helpers=array('Html','Ajax','Javascript','Form');
var $components = array( 'RequestHandler','Email');
var $uses=array('Form','User','Attribute');
//Function for login
function login()
{
$email_id=$this->data['User']['email_id'];
$password=$this->data['User']['password'];
$login=$this->User->find('all');
$this->set('Forms',$this->Form->find('all'));
foreach($login as $form):
if($email_id==$form['User']['email_id'] && $password==$form['User']['password'])
{
$this->Session->setFlash('Login Successful');
// $this->render('forms/homepage');
$this->redirect('/forms/homepage');
break;
}
else
{
$this->Session->setFlash('Login UnSuccessful');
//$this->redirect('/forms/index');
}
endforeach;
}
//Function to register the user into Users Table.
function register()
{
$this->set('Forms',$this->Form->find('all'));
if (!empty($this->data))
{
if ($this->User->save($this->data))
{
$this->Session->setFlash('You have been registered.');
$this->set('register', $this->Form->find('all'));
//$this->render('homepage');
$this->redirect('/forms/homepage');
}
}
}
}
是的,现在我正在使用两个控制器,一个用于用户,一个用于表单。但是有一个疑问。我已经运行了上面的代码。实际上在我的 /forms/index.ctp
<?php
echo $javascript->link('prototype.js');
echo $javascript->link('scriptaculous.js');
echo $html->css('main.css');
?>
表单构建器
登记
<?php
echo $form->create('User',array('action'=>'register'));
echo $form->input('User.name');
echo $form->input('User.email_id');
echo $form->input('User.password');
echo $form->end('Register');
?>
</div>
<div id="login">
<h3>Login</h3>
<?php
echo $form->create('User',array('action'=>'login'));
echo $form->input('User.email_id');
echo $form->input('User.password');
echo $form->end('Login');
?>
</div>
我已经注册并登录了..它进入/forms/homepage 但问题是..它没有显示我尝试使用渲染选项的Flash消息(登录成功/未成功),但甚至显示错误..注意:因为我不需要在我的视图/用户/login.ctp 中有任何内容,所以我没有 login.ctp ..(因为我登录并直接移动到我的表单控制器的主页。