CakePhP链接

时间:2012-10-01 00:48:44

标签: cakephp hyperlink

大家好我有一个cakephp页面,当你将鼠标悬停在一个链接上时会显示正确的链接,但是当你点击链接时它会把你带到一个完全不同/错误的页面。

我认为我的观点存在错误,因此我会在此处包含网站的相关部分,add link正在吸引用户fields/view而不是fields/add_new

        <tr>
                <td align='center'><?php echo $templates['Template']['name'] ;?></td>
            <td align='center'><?php echo $templates['Template']['description']; ?> </td>
                <td align='center'>
                    <?php echo $this->Form->Html->link('Add', array('controller' => 'Fields','action'=>'add_new',$templates['Template']['id'])); ;?> |
                    <?php echo $this->Form->Html->link('View', array('controller' => 'Fields','action'=>'view',$templates['Template']['id'])); ;?> |
                    <?php echo $this->Form->Html->link('Edit', array('controller' => 'Templates','action'=>'edit',$templates['Template']['id'])); ;?> |
                    <?php echo $this->Form->Html->link('Delete', array('controller' => 'Templates','action'=>'delete',$templates['Template']['id'])); ;?></td> 
         <tr>





function add_new($id=null){
        //allows users to add another field to an existing template
        $this->set('title_for_layout', 'Create Fields');
        $this->set('stylesheet_used', 'homestyle');
        $this->set('image_used', 'eBOXLogoHome.png');
        $this->layout='home_layout';


        if(($this->Field->save($this->request->data)))
        {

        $id = $this->data['Field']['template_id'];

        $this->set('id',$id);
        $this->redirect(array('controller'=>'Fields', 'action'=>'view',$id));

        }
            $this->set('id',$id);
        }

2 个答案:

答案 0 :(得分:0)

link函数是HtmlHelper类的一部分。因此,您需要将函数调用中的->Form移除到:

<?php echo $this->Html->link('Add', array('controller' => 'fields', 'action'=>'add_new',$templates['Template']['id'])); ;?>

此外,Cake约定要求控制器名称为小写,因此请将控制器指定为:

('controller' => 'fields',

而不是

('controller' => 'Fields',

答案 1 :(得分:0)

function add_new($id=null){
        //allows users to add another field to an existing template
        $this->set('title_for_layout', 'Create Fields');
        $this->set('stylesheet_used', 'homestyle');
        $this->set('image_used', 'eBOXLogoHome.png');
        $this->layout='home_layout';


        //this sets the parameter as a variable 
        $this->set('id', $id);
        //if the data posts to the databse
        if($this->request->is('post'))
        {
            //creates an instance of field in the database
            $this->Field->create(); 
            //if the field saves to the database
            if ($this->Field->save($this->request->data))
            {   //if the user clicks this button
                $id = $this->data['Field']['template_id'];

                $this->set('id',$id);
                $this->redirect(array('controller'=>'Fields', 'action'=>'view',$id));


            }

        }

    }