编程语言
首页 > 编程语言> > php-Silverstripe URL模式$url_handlers不起作用-名称空间似乎冲突

php-Silverstripe URL模式$url_handlers不起作用-名称空间似乎冲突

作者:互联网

当我尝试使用以下$url_handlers为RESTful API创建路由时,似乎在两种模式之间产生了冲突.

class API extends Controller {

    ...

    private static $url_handlers = array(
        'GET object' => 'read',
        'POST object' => 'create',
        'PUT object/$ID' => 'update',
        'PUT object/$ID/$OtherID' => 'assign',
        'DELETE object/$ID' => 'delete',
        'DELETE object/$ID/$OtherID' => 'unassign',
    );

    ...
}

object / 1工作正常,但object / 1/1与更新操作匹配.

我要添加什么额外的细节才能使这些模式起作用?

解决方法:

我在7000和zippy和Flamerohr的帮助下找到了答案

URL模式应按照最具体到最不具体的顺序进行定义.

选项1:

正确地重新排列模式,并在变量之间添加一个静态段,以便增加的特异性避免了模式在例如

private static $url_handlers = array(
    'GET object' => 'read',
    'POST object' => 'create',
    'PUT object/$ID/static-segment/$OtherID' => 'assign',
    'PUT object/$ID' => 'update',
    'DELETE object/$ID/static-segment/$OtherID' => 'unassign',
    'DELETE object/$ID' => 'delete',
);

选项2:

重新正确排列图案,然后使用!定义必须提供URL参数以匹配模式的约定,例如

private static $url_handlers = array(
    'GET object' => 'read',
    'POST object' => 'create',
    'PUT object/$ID/$OtherID!' => 'assign',
    'PUT object/$ID' => 'update',
    'DELETE object/$ID/$OtherID!' => 'unassign',
    'DELETE object/$ID' => 'delete',
);

可以在Silverstripe的Routing文档中找到此约定:
https://docs.silverstripe.org/en/3.2/developer_guides/controllers/routing/#url-patterns

标签:silverstripe,php,routing
来源: https://codeday.me/bug/20191026/1934034.html