问题
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.
回答1:
The router matches routes in reverse order of their declaration. Given the request url /news
, the router will attempt to match first against the route :slug/:page
and, of course, finds a match, so it never gets to examine your news/:page
route.
The solution is to reverse the order in which you declare the routes. Generally speaking, one wants to add generic routes before specific ones.
回答2:
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:
- Edit the "module.config.php" file to have a easy to read code.
- Create a DefineRoutes.php file
- 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
来源:https://stackoverflow.com/questions/35432241/zend-static-and-dynamic-route