有一个没有框架的空项目,但是有一个下载的symfony/router
. 这是路由本身:
<?php
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
$routes = new RouteCollection();
$routes->add('contacts_all', new Route('/api/v1/contacts', ['_controller' => 'Controllers\ContactController::index']));
$routes->add('contact_show', new Route('/api/v1/contacts/{contact}', ['_controller' => 'Controllers\ContactController::show']));
$routes->add('contact_delete', new Route('/api/v1/contacts/{contact}', ['_controller' => 'Controllers\ContactController::delete'], [], [], '', ['DELETE']));
$routes->add('api_v1', new Route('/api/{any}', ['_controller' => 'Controllers\AppController::errorApi'], ['any' => '.*']));
$routes->add('index', new Route('/', ['_controller' => 'Controllers\AppController::index']));
$routes->add('any', new Route('/{any}', ['_controller' => 'Controllers\AppController::index'], ['any' => '.*']));
return $routes;
DELETE 方法不起作用,可能是什么问题?
PS:总的来说,我有一个解决方案,但我不喜欢它:
<?php
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
$routes = new RouteCollection();
switch($_SERVER['REQUEST_METHOD'])
{
case 'GET':
$routes->add('contacts_all', new Route('/api/v1/contacts', ['_controller' => 'Controllers\ContactController::index']));
$routes->add('contact_show', new Route('/api/v1/contacts/{contact}', ['_controller' => 'Controllers\ContactController::show']));
$routes->add('api_v1', new Route('/api/{any}', ['_controller' => 'Controllers\AppController::errorApi'], ['any' => '.*']));
$routes->add('index', new Route('/', ['_controller' => 'Controllers\AppController::index']));
$routes->add('any', new Route('/{any}', ['_controller' => 'Controllers\AppController::index'], ['any' => '.*']));
break;
case 'DELETE':
$routes->add('contact_delete', new Route('/api/v1/contacts/{contact}', ['_controller' => 'Controllers\ContactController::delete']));
break;
}
return $routes;
由于在前面的路由中没有明确指定方法,因此接受任何方法。并且该路由
contact_show
适用于 GET 和 DELETE,因为模式是相同的。提升路由contact_delete
高于contact_show
或显式写入contact_show
GET 方法您缺少一个参数。在
['DELETE']
插入另一个空数组之前