Zend - 静态和动态路由


Zend - static and dynamic route

我应该如何准备我的路由来处理它,而不是url中的成瘾部分?

$routes = array(
/**
 * Static
 */
'news' => new Zend_Controller_Router_Route('news/:page',
    array('controller' => 'news', 'action' => 'index', 'page' => 1 )
),
/**
 * Dynamic
 */
'thread' => new Zend_Controller_Router_Route(':slug/:page',
    array('controller' => 'Thread', 'action' => 'index', 'page' => 1 )
),

例如 example.com/线程名称 它显示带有 slug 线程名称-slug 的线程,但当我访问 example.com/news 时,它想显示带有 slug 新闻的线程。我想要这里的静态页面。

提前谢谢。

路由器按其声明的相反顺序匹配路由。给定请求 url /news ,路由器将首先尝试与路由:slug/:page匹配,当然,找到匹配项,因此它永远不会检查您的news/:page路由。

解决方案是反转声明路由的顺序。一般来说,人们希望在特定路由之前添加通用路由。

由于最新版本的 zendframework 是 3.x,我将发布一个 Zf3 的示例解决方案,因为一篇关于 zend 路由的完整文章并不容易。

假设您希望仅使用一个控制器来集中管理请求;因此您可以检查权限,角色等,以便为站点的管理页面提供服务。我们将执行以下任务:

  1. 编辑"module.config.php"文件以获得易于阅读的代码。
  2. 创建定义路由.php文件
  3. 编写一个简单的正则表达式来为所有可能的管理任务设置通配符匹配位置。

我将支持我们创建一个在"modules.config.php"文件中正确注册的管理模块

正在编辑 module.config.php 文件:

<?php
/**
 * @Filename: zendframework/module/Admin/config/module.config.php
 * The module required settings.
 * @author: your name here
*/
return [
  'controllers' => [
    'factories' => include __DIR__ . '/ControllerFactories.php'
  ],
  'router'      => [
    'routes' => include __DIR__ . '/DefineRoutes.php',
  ],
  'view_manager' => ['template_path_stack' => [__DIR__ . '/../view',],],
];

注意:我们在文件中不使用关闭标签?>

创建"定义路由.php"文件。

<?php
/**
 * @Filename: zendframework/module/Admin/config/DefineRoutes.php
 * Declares site's admin routes
 * @author: your name here
*/
namespace Admin;
use Zend'Router'Http'Segment;
// first a couple of useful functions to make our life easy:
// Creates a regexp to match all case-variants of a word
function freeCaseExt($toCase){
    $len = strlen($toCase);
    $out = '';
    if($len < 1){ return $out; }
    for ($i=0; $i<$len; $i++){
        $s = strtolower(substr($toCase, $i, 1));
        $out .= '['.$s.strtoupper($s).']';
    } 
    return $out;
}
// To append slash child routes elsewhere
function SlashUri($controller, $action){
    return [
        'type' => 'Zend'Router'Http'Literal::class, 
        'options' => [
            'route' => '/', 
            'defaults' => ['controller' => $controller, 'action' => $action ]]];
}
$adminvariants = freeCaseExt('admin'); // to constrain our main route
// Our route family tree:
'admin' => [
    'type'    => Segment::class,
    'options' => [
        'route'     => '/:admin[/:case][/:casea][/:caseb][/:casec][/:cased][/:casee][/:casef][/:caseg][/:caseh]',
        'constraints' => [ 
          'admin' => $adminvariants,
          'case'  => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
          'casea' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
          'caseb' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
          'casec' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
          'cased' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
          'casee' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
          'casef' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
          'caseg' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
          'caseh' => '[a-zA-Z0-9][a-zA-Z0-9_-]*'
        ],
        'defaults'  => [
            'controller' => Controller'AdminController::class,
            'action'     => 'index'
        ]
    ],
    'may_terminate' => TRUE,
    'child_routes' => [
        'adminslash' => SlashUri(Controller'AdminController::class, 'index'),
    ]
],
// Now you have declared all the posible admin routes with or without
// slaches '/' at 9 deep levels using the AdminController::Index() method 
// to decide wath to do.

重要提示:当我们定义第一级通配符:admin时,需要适当的约束,否则它与其他第一级路由重叠。

控制器逻辑是一些不合时宜的。希望这个想法对某人有所帮助。

路易斯