【发布时间】:2020-01-05 20:36:49
【问题描述】:
我正在玩路由。我想要实现的是像“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'));
}
查看生成的 URL 代码
<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>
所以看起来它应该发布到正确的位置(customer4 是记录的 slug)。
问题是为什么Cake在请求编辑页面时能够获取到正确的路由,但是在发帖时却选择了不同的路由?
注意:我认为还有其他一些路由错误,好像我删除了后备,然后我的 / 路由不起作用。
对于上下文,以下工作正常:
/customer/add 用于编辑和表单提交。
CakePHP 版本为 3.8.8
【问题讨论】:
标签: cakephp cakephp-3.0