phpcakephpcakephp-2.6

How to point to a different Controller of a Model in CakePHP


I have one Model class named 'Name' and one Controller class named 'Controller1'. I have created a form using 'Name' model which is by default pointing to 'names' controller as per default naming convention of cakephp. But i want it to point to 'Controller1' controller..

I've replace this code:-

<?php echo $this->Form->create('Name'); ?>

with this one to see if it works:-

<?php 
     echo $this->Form->create('Name',array('controller'=>'Controller1', 
    'action'=>'view')); 
?>

But it is not working and is pointing to 'names' controller only. enter image description here

How can i point it to 'Controller1' instead of 'names' controller?

More details:- Model (Name.php):-

class Name extends AppModel{
    public $useTable = "tbl_names";
}

Controller (Controller1Controller.php):-

class Controller1Controller extends AppController
{    
    public function index(){

    }

    public function view($id){
        $name ='';
        if($this->request->is('post')){
            $name = $this->request->data('name');
            if($name==="xyz"){
                $this->redirect('http://www.google.com');
            }else{
                $this->Save($this->request->data);
                $this->request->data['name']= 'Nice';
            }
        }
        return $id;
    }

    // Save to database if entered name is other than 'xyz'
    private function Save($data){
        $this->Name->create();
    }

    public function viewname($id,$name){

    }
}

Solution

  • You have to set url key to pass crontroller and action values.

    echo $this->Form->create(false, array(
        'url' => array('controller' => 'recipes', 'action' => 'add'),
        'id' => 'RecipesAdd'
    ));
    

    See FormHelper > Options for create()