Zend - 静态和动态路由 [英] Zend - static and dynamic route

查看:29
本文介绍了Zend - 静态和动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$routes = array(/*** 静止的*/'新闻' =>new Zend_Controller_Router_Route('news/:page',数组('控制器' => '新闻','动作' => '索引','页面' => 1 )),/*** 动态的*/'线程' =>new Zend_Controller_Router_Route(':slug/:page',数组(控制器"=>线程",动作"=>索引",页面"=> 1)),

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

提前致谢.

解决方案

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

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

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

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

编辑module.config.php文件:

['工厂' =>包括 __DIR__ .'/ControllerFactories.php'],'路由器' =>['路线' =>包括 __DIR__ .'/DefineRoutes.php',],'view_manager' =>['template_path_stack' =>[__DIR__ .'/../看法',],],];

注意:我们的文件中不使用结束标签 ?>

创建DefineRoutes.php"文件.

\Zend\Router\Http\Literal::class,'选项' =>['路线' =>'/','默认' =>['控制器' =>$controller, '动作' =>$动作]]];}$adminvariants = freeCaseExt('admin');//约束我们的主路由//我们的路由家族树:'管理员' =>['类型' =>段::类,'选项' =>['路线' =>'/:admin[/:case][/:casea][/:caseb][/:casec][/:cased][/:casee][/:casef][/:caseg][/:caseh]','约束' =>['管理员' =>$adminvariants,'案例' =>'[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_-]*','外壳' =>'[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_-]*'],'默认' =>['控制器' =>控制器\管理控制器::类,'动作' =>'指数']],'may_terminate' =>真的,'child_routes' =>['adminslash' =>SlashUri(Controller\AdminController::class, 'index'),]],//现在你已经声明了所有可能的管理路由,有或没有//使用 AdminController::Index() 方法在 9 个深层次上 slaches '/'//决定做什么.

重要提示:当我们定义第一级通配符:admin时,一个适当的约束必需或者它与其他第一级路由重叠.

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

路易斯

How should I prepare my routes to deal with it, instead of addictional parts in 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 )
),

e.g. example.com/thread-name-slug it shows thread with slug thread-name-slug but when I visit example.com/news it wants to show thread with slug news. I want static page here.

Thanks in advance.

解决方案

As the latest version of zendframework is 3.x I'll post a sample solution for Zf3, because it's not easy a complete article on zend routes.

Supouse you wanna centralize your admin requests by using only one controller; so you can check permisions, roles, etc in order to serve your site's admin pages. We'll perform the next tasks:

  1. Edit the "module.config.php" file to have a easy to read code.
  2. Create a DefineRoutes.php file
  3. Write a simple regular expression to set wildcard matching places for all posible admin tasks.

I'll supouse we creates an admin module properly registered in "modules.config.php" file

Editing the module.config.php file:

<?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',],],
];

Note: we do not use the close tag ?> in our files

Creating the "DefineRoutes.php" file.

<?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.

IMPORTANT: As we defined a first level wildcard :admin a proper constraint is required or it overlaps other first level routes.

The controllers logics is a few out of skope. Hope this idea helps somebody.

Luis

这篇关于Zend - 静态和动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆