0

基于这个stackoverflow问题15077075,我已经完成了我的应用程序有一个基于正则表达式的路由,所以我可以将它传递给我的视图和编辑操作。

  • app/123 - 应用控制器视图操作(通过文章 id 获取)
  • app/name-of-article - 应用控制器视图操作(通过文章名称获取)
  • app/123/edit - 应用控制器编辑操作(文章 ID)
  • app/name-of-article/edit - 应用控制器编辑操作(文章名称)
  • app/search/{search-string} - 应用程序控制器搜索操作(目前只接受不带空格和特殊字符的搜索字符串)

这是我用下面的代码完成的,类似于上面链接中的代码:

'app' => array(
    'type'    => 'literal',
    'options' => array(
        'route'    => '/app',
        'defaults' => array(
            'controller' => 'App\Controller\App',
            'action'     => 'index',
        ),
    ),
    'may_terminate' => true,
),
'view' => array(
    'type' => 'regex',
    'options' => array(
        'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)',
        'defaults' => array(
            'controller' => 'App\Controller\App',
            'action' => 'view',
        ),
        'spec' => '/app/%view%',
    ),
    'priority' => -1000,
),
'edit' => array(
    'type' => 'regex',
    'options' => array(
        'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)/(?<edit>[a-zA-Z0-9_-]+)',
        'defaults' => array(
            'controller' => 'App\Controller\App',
            'action' => 'edit',
        ),
        'spec' => '/app/%view%/%edit%',
    ),
    'priority' => -1000,
),

我有两个问题,第一个是 url viewhelper 无法识别我的路线

$controller = app
$action = recent
$this->url( $controller, array( 'action' => $action ) )

它只是打印 /app 而不是 /app/recent,

$action = 'search'$action = 'new'仅打印 /app时也会发生同样的情况


第二个是只有当我在其中输入空格或特殊字符时,搜索才会被其控制器操作识别

当我尝试在搜索键('[\sa-zA-Z0-9_-]+')的约束中添加 \s 时,它会路由到编辑功能

搜索的路线是这样的

编辑了到这个的路线并且它有效

'search' => array(
    'type' => 'literal',
    'options' => array(
        'route' => '/app/search',
        'defaults' => array(
            'controller' => 'App\Controller\App',
            'action'     => 'search',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'search' => array(
            'type' => 'segment',
            'options' => array(
                'route' => '/[:searchkey]',
                'constraints' => array(
                    'searchkey' => '[a-zA-Z0-9_\+-]+'
                ),
                'defaults' => array(
                    'action' => 'search'
                ),
            ),
        ),
    ),
),

如果需要任何代码片段,我希望很清楚我想要什么,请询问

4

1 回答 1

0

整理好了!
如果需要以这种方式在他的应用程序中获取路由,您可以使用下面的代码

/App                        App/Index
/App/name-of-article        App/View
/App/123                    App/View
/App/name-of-article/edit   App/Edit
/App/123/edit               App/Edit
/App/recent                 App/Recent
/App/search                 App/Search
/App/new                    App/New

在我的module.config.php我改变了路线:

'app' => array(
    'type' => 'segment',
    'options' => array(
        'route' => '/app[/:action]',
        'constraints' => array(
            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id' => '[0-9]+',
        ),
        'defaults' => array(
            'controller' => 'App\Controller\App',
            'action' => 'index',
        ),
    ),
    'may_terminate' => true,
),

'search' => array(
    'type' => 'literal',
    'options' => array(
        'route' => '/app/search',
        'defaults' => array(
            'controller' => 'App\Controller\App',
            'action' => 'search',
        ),
    ),
    'priority' => 1,
    'may_terminate' => true,
    'child_routes' => array(
        'search' => array(
            'type' => 'segment',
            'options' => array(
                'route' => '/[:searchkey]',
                'constraints' => array(
                    'searchkey' => '[\sa-zA-Z0-9_\+-]+'
                ),
                'defaults' => array(
                    'action' => 'search'
                ),
            ),
            'priority' => 2,
            'may_terminate' => true,
        ),
    ),
),

'new' => array(
    'type' => 'literal',
    'options' => array(
        'route' => '/app/new',
        'defaults' => array(
            'controller' => 'App',
            'action' => 'recent',
        ),
    ),
    'priority' => 2,
),

'view' => array(
    'type' => 'regex',
    'options' => array(
        'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)',
        'defaults' => array(
            'controller' => 'App\Controller\App',
            'action' => 'view',
        ),
        'spec' => '/app/%view%',
    ),
),

'edit' => array(
    'type' => 'regex',
    'options' => array(
        'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)/(?<edit>[a-zA-Z0-9_-]+)',
        'defaults' => array(
            'controller' => 'App\Controller\App',
            'action' => 'edit',
        ),
        'spec' => '/app/%view%/%edit%',
    ),
),

如果您需要其他路线,您可以在 te 路线下方添加一个新的文字路线app,确保您不要忘记设置priority

于 2014-06-04T08:56:08.350 回答