基于这个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'
),
),
),
),
),
如果需要任何代码片段,我希望很清楚我想要什么,请询问