如何在 CakePHP 中指向模型的不同控制器
How to point to a different Controller of a Model in CakePHP
我有一个名为 'Name' 的模型 class 和一个名为 'Controller1' 的控制器 class。我使用 'Name' 模型创建了一个表单,根据 cakephp 的默认命名约定,该模型默认指向 'names' 控制器。但我希望它指向 'Controller1' 控制器..
我已经替换了这段代码:-
<?php echo $this->Form->create('Name'); ?>
用这个看看它是否有效:-
<?php
echo $this->Form->create('Name',array('controller'=>'Controller1',
'action'=>'view'));
?>
但它不工作,仅指向 'names' 控制器。
如何将它指向 'Controller1' 而不是 'names' 控制器?
更多详情:-
型号 (Name.php):-
class Name extends AppModel{
public $useTable = "tbl_names";
}
控制器(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){
}
}
您必须设置 url 键才能传递控制器和操作值。
echo $this->Form->create(false, array(
'url' => array('controller' => 'recipes', 'action' => 'add'),
'id' => 'RecipesAdd'
));
我有一个名为 'Name' 的模型 class 和一个名为 'Controller1' 的控制器 class。我使用 'Name' 模型创建了一个表单,根据 cakephp 的默认命名约定,该模型默认指向 'names' 控制器。但我希望它指向 'Controller1' 控制器..
我已经替换了这段代码:-
<?php echo $this->Form->create('Name'); ?>
用这个看看它是否有效:-
<?php
echo $this->Form->create('Name',array('controller'=>'Controller1',
'action'=>'view'));
?>
但它不工作,仅指向 'names' 控制器。
如何将它指向 'Controller1' 而不是 'names' 控制器?
更多详情:- 型号 (Name.php):-
class Name extends AppModel{
public $useTable = "tbl_names";
}
控制器(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){
}
}
您必须设置 url 键才能传递控制器和操作值。
echo $this->Form->create(false, array(
'url' => array('controller' => 'recipes', 'action' => 'add'),
'id' => 'RecipesAdd'
));