views:

890

answers:

1

Hi, I m new to cakephp... I m developing an app using CakePhp and Mysql. In Cakephp i am having two tables one is Form (id ,name,created , modified) and attributes (id, Attribute-name ,form_id ,value) I kept everything in only one controller that is in forms controller.Within this formsctrller , i m having actions like login ,register,viewforms...

 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...

Edit

<?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');
  }
 }
}

}

Ya Now i am using two controllers One for Users and One for Forms . But One Doubt .I have run the above code..Actually in my /forms/index.ctp

      <?php
     echo $javascript->link('prototype.js');
     echo $javascript->link('scriptaculous.js');
    echo $html->css('main.css');
   ?>

formBuildr

Register

          <?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>

I have registered and login ..It goes /forms/homepage But the thing is that ..It doesn't show me the Flash message Of (Login successfull/Unsucessfull) I tried with render option , but even showing me the error.. Note :Since i no need to have anything in my views/users/login.ctp i am not having login.ctp..(SINCE I M LOGIN IN AND DIRECTLY MOVES TO HE HOMEPAGE OF MY FORMSCONTROLLER.

+2  A: 

You obviously haven't done your homework. I suggest you read Cake's official Cookbook.

To answer your question. If you want to handle login and user registration, you should actually

  1. Create a new table called users (id, username, password)
  2. Create class User extends AppModel as app/models/user.php
  3. Create class UsersController extends AppController as app/controllers/users_controller.php
  4. You should put login() and register() in your UsersController.

Now, to switch from one controller to another controller, you provide a hyperlink.

<a href="/cake/users">Users</a>
<a href="/cake/forms">Manage your forms</a>

Or, a more Cake-ish approach

<?php echo $html->link("Users", '/users');?>
<?php echo $html->link("Manage your forms", '/forms');?>

Serious though, if you have further questions, you're advised to RTFM.

Edit: If submit an HTML form from /forms to /users, there's no automagic redirection. So if you want the users go be redirected, you'd need to do this

<?php 

class UsersController extends AppController
{
    function login()
    {
        if(!empty($this->data))
        {
            // Process $_POST

            // Finally redirect the user back to forms
            $this->redirect('/forms');
        }
    }
}

?>
kizzx2
<div id="register"><h3>Register</h3><?phpecho $form->create('Form',array('action'=>'register'));echo $form->input('User.name');echo $form->input('User.email_id');echo $form->input('User.password');echo $form->end('Register');?></div>Actually this my index.ctp in /formson clicking on this register what i need is the same what u said If i kept userscontroller ,will it directly wents there to usercontroller and come back to forms .to design forms
Aruna
If you want to add additional information, try doing _Edit_ on the main question instead of posting messy code fragments in comments. I have updated my reply to answer your question.
kizzx2

related questions