2016-02-16 123 views
0

我應該如何準備我的路由來處理它,而不是url中的成癮部分?Zend - 靜態和動態路由

$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/ 線程名段塞它顯示了塞線程名段塞線程,但是當我參觀example.com/ 新聞它要顯示出與塞新聞線程。我想在這裏靜態頁面。

在此先感謝。

+0

你想要達到什麼目的,只處理動態路由,沒有任何靜態路由? – RomanPerekhrest

回答

0

路由器匹配的路由反向其聲明的順序。鑑於請求的URL /news,路由器將嘗試匹配第一個路由:slug/:page,當然,找到一個匹配,所以它永遠不會檢查您的news/:page路線。

解決方法是顛倒您聲明路線的順序。一般來說,人們希望在特定路線之前添加通用路線。

0

由於zendframework的最新版本是3.x,我會發布Zf3的樣本解決方案,因爲在zend路由上這是一篇不完整的文章。

您希望通過僅使用一個控制器來集中管理請求,所以你可以檢查permisions,角色等,以便爲您的網站的管理頁面。 我們將執行下一個任務:

  1. 編輯「module.config.php」文件以獲得易於閱讀的代碼。
  2. 創建一個DefineRoutes.php文件
  3. 編寫一個簡單的正則表達式來爲所有可能的管理任務設置通配符匹配位置。

我會supouse我們創建了一個管理模塊中的 「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',],], 
]; 

注:我們做的不使用close標籤? >在我們的文件中

創建「DefineRoutes.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. 

重要:由於我們所限定的第一級通配符:管理員適當約束是需要或它重疊其它第一級路由。

控制器邏輯是幾個skope。 希望這個想法可以幫助某人。

Luis