用于表单提交的 Cakephp3 前缀路由不起作用



我在玩路由。 我想要实现的是像"customer/customer_slug/..."这样的URL,所以,它将是/customer/test/hosts/add或/customer/acme/networks/list。 我设置了以下路线...

Router::prefix('customer', function (RouteBuilder $routes){
$routes->connect('/',['controller' => 'Customers', 'action' => 'index']);
//    ->setMethods(['GET','POST']);
$routes->connect('/add',['controller' => 'Customers', 'action' => 'add'])
->setMethods(['GET','POST']);
$routes->connect('/:slug/edit',['controller' => 'Customers', 'action' => 'edit'])
->setMethods(['GET','POST'])
->setPatterns(['slug'=>'[a-z0-9-_]+'])
->setPass(['slug']);
$routes->connect('/:slug',['controller' => 'Customers', 'action' => 'overview'])
->setMethods(['GET','POST'])
->setPatterns(['slug'=>'[a-z0-9-_]+'])
->setPass(['slug']);
$routes->connect('/:slug/:controller', ['action' => 'index'],['routeClass' => DashedRoute::class])
->setMethods(['GET','POST'])
->setPatterns(['slug'=>'[a-z0-9-_]+'])
->setPass(['slug']);
$routes->connect('/:slug/:controller/:action', ['param'=>'slug'],['routeClass' => DashedRoute::class])
->setMethods(['GET','POST'])
->setPatterns(['slug'=>'[a-z0-9-_]+'])
->setPass(['slug']);
$routes->connect('/:slug/:controller/:action/*', [],['routeClass' => DashedRoute::class])
->setMethods(['GET','POST'])
->setPatterns(['slug'=>'[a-z0-9-_]+'])
->setPass(['slug']); 
// TODO:  Understand and look to remove this 
$routes->fallbacks(DashedRoute::class);
});

我可以在测试中转到 URL,这很好。 但是,当我转到/customer/acme/edit 时,我在 Customer/CustomersController 上获得了编辑操作.php它会显示相应的表单。 查看路由是调试工具包,它说它正在使用以下路由:

customer:customers:edit /customer/:slug/edit    
{
"controller": "Customers",
"action": "edit",
"prefix": "customer",
"plugin": null,
"_method": [
"GET",
"POST"
]
}

这就是我所期望的。 但是,当我提交表单时,它将路由更改为:

customer:_controller:_action    /customer/{controller}/{action}/*   
{
"prefix": "customer",
"plugin": null,
"action": "index"
}

如果我从前缀部分删除$routes->fallbacks(...,则会收到CSRF错误。

我的编辑页面很简单

<?php $this->extend('../../Layout/TwitterBootstrap/dashboard'); ?>
<?php $this->start('tb_actions'); ?>
<li><?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $customer->id], ['confirm' => __('Are you sure you want to delete # {0}?', $customer->id), 'class' => 'nav-link']) ?></li>
<li><?= $this->Html->link(__('List Customers'), ['action' => 'index'], ['class' => 'nav-link']) ?></li>
<?php $this->end(); ?>
<?php $this->assign('tb_sidebar', '<ul class="nav flex-column">' . $this->fetch('tb_actions') . '</ul>'); ?>
<div class="customers form content">
<?= $this->Form->create($customer) ?>
<fieldset>
<legend><?= __('Edit Customer') ?></legend>
<?php
echo $this->Form->control('name');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

来自控制器的关联代码是

public function edit($slug = null)
{
$customer = $this->Customers->getbySlugID($slug);
if ($this->request->is(['patch', 'post', 'put'])) {
$customer = $this->Customers->patchEntity($customer, $this->request->getData());
if ($this->Customers->save($customer)) {
$this->Flash->success(__('The customer has been saved.'));
exit;
return $this->redirect(['controller'=>false, 'action' => 'index', $customer->slug]);
}
$this->Flash->error(__('The customer could not be saved. Please, try again.'));
}
$this->set(compact('customer'));
}

查看生成的网址代码

<form method="post" accept-charset="utf-8" role="form" action="/customer/customer4/edit"><div style="display:none;"><input type="hidden" name="_method" value="PUT"></div>    <fieldset>
<legend>Edit Customer</legend>
<div class="form-group text required"><label for="name">Name</label><input type="text" name="name" required="required" maxlength="40" id="name" class="form-control" value="Customer4"></div>    </fieldset>
<button type="submit" class="btn btn-secondary">Submit</button>    </form>

所以看起来它应该发布到正确的位置(客户 4 是记录的 slug(。

问题是为什么 Cake 在请求编辑页面时能够获得正确的路线,但在发布时,选择不同的路线?

注意:我认为还有其他一些路由错误,例如如果我删除了回退,那么我的/路由就不起作用。

对于上下文,以下内容工作正常:

/customer/add 用于编辑和表单提交。

蛋糕PHP版本是3.8.8

CakePHP 表单默认使用(模拟(PUT进行更新(请参阅生成的表单中的隐藏_method字段。是的,这是否应该更好PATCH是有争议的,但事实就是如此(,并且您将路由限制为GETPOST,因此它们在提交表单时都不会匹配,并且后备路由将匹配请求,因为它们不应用任何 HTTP 方法限制。

长话短说,请确保您的/edit路线也至少接受PUT

$routes
->connect('/:slug/edit',['controller' => 'Customers', 'action' => 'edit'])
->setMethods(['GET', 'POST', 'PUT']) // <<<<<< there
->setPatterns(['slug'=>'[a-z0-9-_]+'])
->setPass(['slug']);

您的控制器也接受PATCH,这是 Bake 创建的默认值,但如果您实际上不使用或不想接受PATCH请求,那么您当然可以放弃它。

参见

  • CakePHP API> \Cake\View\Helper\FormHelper::create((

最新更新