通过表单将参数传递给控制器(CakePHP)

通过表单将参数传递给控制器(CakePHP),php,cakephp,forms,parameters,Php,Cakephp,Forms,Parameters,好吧,我对CakePHP还不太熟悉。以下是设置: 我有一个模型(预订)和一个控制器(ReservationController) 我试图提供一个简单的add()功能 请求url为:www.example.com/reservations/add/3 其中3是该预订的活动ID。 到目前为止,一切顺利。问题是,表单是这样构造的: <h2>Add reservation</h2> <?php echo $form->create('Reservation'); e

好吧,我对CakePHP还不太熟悉。以下是设置:

我有一个模型(预订)和一个控制器(ReservationController)

我试图提供一个简单的add()功能

请求url为:www.example.com/reservations/add/3

其中3是该预订的活动ID。 到目前为止,一切顺利。问题是,表单是这样构造的:

 <h2>Add reservation</h2>
<?php echo $form->create('Reservation');
echo $form->input('first_name');
echo $form->input('last_name');
echo $form->input('email');
echo $form->input('tickets_student'); echo $form->input('tickets_nstudent');
echo $form->end('Add');?>
然后,以以下形式:

// make it available for the form
$this->set('event_id', $eventid);
$form->create('Reservation',array('url' => array($event_id)));

这很管用,但我觉得有点难看。有没有一种更简单的方法来确保表单POST操作是针对当前url的,而不是针对没有id的url的?

遵循最严格的惯例,阅读像
/reservations/add/3
这样的url会让人困惑。您正在调用
reservationscoontroller
Reservation
模型执行操作,但向其传递了一个事件ID。调用
/reservations/edit/3
的混乱程度要小得多,但对于您的情况来说同样错误,因为
ID
值“3”将被假定为预订标识符

本质上,你最多只能提出一个模棱两可的请求。这是一个创建预订的简单表单,该预订必须与事件关联。在“传统”场景中,表单允许用户从某种列表中选择事件。毕竟,外键,在本例中可能是
event\u id
,实际上只是保留的另一个属性。必须在控制器中填充该列表;这通常通过
find('list')
调用完成


如果您已经知道要创建保留所针对的事件(您显然知道),那么我可能会选择类似的方法,在表单中使用隐藏字段。您仍然需要像现在这样在控制器中设置值,但最终的结果是更简单。隐藏的字段将命名为
data[Reservation][event\u id]
(同样,我假设您的外键字段的名称)。

按照最严格的约定,阅读像
/reservations/add/3
这样的URL会让人困惑。您正在调用
reservationscoontroller
Reservation
模型执行操作,但向其传递了一个事件ID。调用
/reservations/edit/3
的混乱程度要小得多,但对于您的情况来说同样错误,因为
ID
值“3”将被假定为预订标识符

本质上,你最多只能提出一个模棱两可的请求。这是一个创建预订的简单表单,该预订必须与事件关联。在“传统”场景中,表单允许用户从某种列表中选择事件。毕竟,外键,在本例中可能是
event\u id
,实际上只是保留的另一个属性。必须在控制器中填充该列表;这通常通过
find('list')
调用完成

如果您已经知道要创建保留所针对的事件(您显然知道),那么我可能会选择类似的方法,在表单中使用隐藏字段。您仍然需要像现在这样在控制器中设置值,但最终的结果是更简单。隐藏字段将命名为
data[Reservation][event\u id]
(同样,我假设您的外键字段的名称)。

您可以执行以下操作:

$form->create('Reservation',array('url' => $this->Html->url()));
这样,url中的所有变量都将添加到表单action:)

您可以执行以下操作:

$form->create('Reservation',array('url' => $this->Html->url()));

这样,url中的所有变量都将添加到表单action:)

正如Rob Wilkerson所建议的,问题在于url路由没有准确描述正在执行的操作。当您想要编辑预订时,它会变得更加混乱:
/reservations/edit/6
。现在URL中的数字表示不同的意思

我在这种情况下使用的URL约定(适合您的特定情况)是
/events/3/reservations/add
。它需要更多的预先配置您的路线,但我发现它的优越性为清晰的道路

示例
routes.php

Router::connect(
    '/events/:event_id/reservations/:action', 
    array('controller'=>'reservations','action'=>'index'),
    array(
        'pass' => array('event_id'), // pass the event_id as a param to the action
        'event_id' => '[0-9]+',
        'actions'=>'add|index' // only reverse-routes with 'action'=>'add' or 
                               // 'action'=>'index' will match this route
    )
)
// Usage: array('controller'=>'reservations','action'=>'add','event_id'=>3)

Router::connect(
    '/events/:event_id/reservations/:id/:action', 
    array('controller'=>'reservations'),
    array(
        'pass' => array('id'), // pass the reservation id as a param to the action
        'id' => '[0-9]+',
        'event_id' => '[0-9]+',
        'actions'=>'edit|delete' // only reverse-routes with 'action'=>'edit' or 
                                 // 'action'=>'delete' will match this route
    )
)
// Usage: array('controller'=>'reservations','action'=>'edit','event_id'=>3,'id'=>6)

在您的
FormHelper::create
调用中,您必须指定您想要遵循的大部分反向路径,但同样,前期成本将在未来产生回报。通常,使用Cake显式比希望其automagic始终正确工作要好,尤其是在应用程序复杂性增加的情况下。

正如Rob Wilkerson所建议的,问题在于您的URL路由没有准确描述正在执行的操作。当您想要编辑预订时,它会变得更加混乱:
/reservations/edit/6
。现在URL中的数字表示不同的意思

$form->create('Reservation',array('action' => 'add',$eventId);
我在这种情况下使用的URL约定(适合您的特定情况)是
/events/3/reservations/add
。它需要更多的预先配置您的路线,但我发现它的优越性为清晰的道路

示例
routes.php

Router::connect(
    '/events/:event_id/reservations/:action', 
    array('controller'=>'reservations','action'=>'index'),
    array(
        'pass' => array('event_id'), // pass the event_id as a param to the action
        'event_id' => '[0-9]+',
        'actions'=>'add|index' // only reverse-routes with 'action'=>'add' or 
                               // 'action'=>'index' will match this route
    )
)
// Usage: array('controller'=>'reservations','action'=>'add','event_id'=>3)

Router::connect(
    '/events/:event_id/reservations/:id/:action', 
    array('controller'=>'reservations'),
    array(
        'pass' => array('id'), // pass the reservation id as a param to the action
        'id' => '[0-9]+',
        'event_id' => '[0-9]+',
        'actions'=>'edit|delete' // only reverse-routes with 'action'=>'edit' or 
                                 // 'action'=>'delete' will match this route
    )
)
// Usage: array('controller'=>'reservations','action'=>'edit','event_id'=>3,'id'=>6)
在您的
FormHelper::create
调用中,您必须指定您想要遵循的大部分反向路径,但同样,前期成本将在未来产生回报。使用Cake显式通常比希望其automagic始终正确工作要好,尤其是当应用程序的复杂性增加时

$form->create('Reservation',array('action' => 'add',$eventId);
在控制器中:

    function add($eventId = null)
    {

            if($eventId == null)
            {
                // error state
                throw new NotFoundException('Invalid id');
            }

    ...

    }
我总是这样做

在控制器中:

    function add($eventId = null)
    {

            if($eventId == null)
            {
                // error state
                throw new NotFoundException('Invalid id');
            }

    ...

    }
我总是这样做。

如果网站不在服务器的公共html根目录中,则会失败

这个答案更加可靠:

<?php echo $form->create('Reservation',array('url'=>'/reservation/add/'.$data['id']));?>

如果网站不在服务器公共\u html根目录中,则将失败

这个答案更加可靠:

<?php echo $form->create('Reservation',array('url'=>'/reservation/add/'.$data['id']));?>


我同意你的看法,因为我在项目中使用相同的表单进行添加和编辑,这会让人困惑,但是如果@Forceflow