【问题标题】:Cake PHP Form HelperCake PHP 表单助手
【发布时间】:2010-12-19 08:56:38
【问题描述】:

我遇到了表单助手的问题。我的控制器名称是posts_controller.php,如下所示

<?php
    Class PostsController extends AppController
    {
        var $name='Posts';
        var $helpers=array('Html','Form','Link');
        var $components = array('Session');
        function index()
        {
            $this->pageTitle='Cake PHP Index page';            
            $this->paginate();
            $this->set('posts',$this->Post->find('all'));
        }

        function view($id=null)
        {
            $this->Post->id = $id;
            $this->set('post', $this->Post->read());
        }

        function add()
        {
            if(!empty ($this->data))
            {
                if($this->Post->save($this->data))
                {
                    $this->Session->setFlash('Your post has been saved.');
                    $this->redirect(array('action' => 'index'));
                }
            }
        }
    }
?>

当我转到添加操作时,视图 add.ctp 会加载相应的表单。

这是我的视图文件add.ctp

<?php
    echo $this->Form->create('Post');
    echo $this->input('title');
    echo $this->input('body');
    echo $this->Form->end('Save');
?>

当我提交表单时,我收到一条错误消息,指出您的控制器中未定义发布操作。当我在浏览器中检查页面的来源时,表单的操作有一个错误的值。我得到的值是

<form id="PostAddForm" method="post" action="/cakephp/app/webroot/index.php/posts/posts/add">

而不是

<form id="PostAddForm" method="post" action="/cakephp/app/webroot/index.php/posts/add">

你能帮帮我吗?

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    尝试在您的服务器上启用 mod_rewrite。对我来说,这就是问题所在,尤其是如果您的代码就是您发布的代码。

    【讨论】:

      【解决方案2】:

      正如 Nik 所说,它看起来像是一个 mod_rewrite 问题。该表单 HTML 应如下所示:

      <form id="PostAddForm" method="post" action="/YourSite/posts/add">
      

      mod_rewrite 是一个 Apache 模块。通常默认情况下不启用它。有很多页面解释了如何做到这一点,但如果你在 Ubuntu 或 Debian 上,这可能有点神秘。如果是这样,请看这里:http://bit.ly/ubuntu_mod_rewrite

      【讨论】:

      • 感谢 Nik 和 Leo,我在尝试使用 mod_rewrite 时已将其纠正。非常感谢...
      【解决方案3】:

      你还忘记了代码中的 ':

       echo $this->input('title);
       echo $this->input('title');
      

      【讨论】:

        【解决方案4】:

        我觉得应该这样写

        $this->Form->create("Post",array('action'=>'add'));
        

        【讨论】:

          猜你喜欢
          • 2013-09-09
          • 1970-01-01
          • 1970-01-01
          • 2012-05-16
          • 2016-12-09
          • 2011-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多