在用zend framework时候定义url重写规则十分方便,也很强大
Zend_Controller_Router_Rewrite是用来处理url重写的基类.
1.怎么用:
getRouter(); // returns a rewrite router by default
$router->addRoute(
‘user’,
new Zend_Controller_Router_Route(’user/:username’, array(’controller’ => ‘user’, ‘action’ => ‘info’))
);
添加一个url重写.意思是当url请求路径符合’user/param’的时候.重定向到user controller的info action上去.而param将作为username的参数.
注意:可以通过$controller->getRequest()->getParam(’username’)取得param的值.不可以通过$_Request取.
2.例子
2.1:重定向到不同的module下面.
‘default’)
);
$router->addRoute(’default’, $route);
2.2:主域名 重写
‘blog.mysite.com’,
‘path’ => ‘archive’
),
array(
‘module’ => ‘blog’,
‘controller’ => ‘archive’,
‘action’ => ‘index’
)
);
$router->addRoute(’archive’, $route);
2.3 Zend_Controller_Router_Route_Static
‘auth’, ‘action’ => ‘login’)
);
$router->addRoute(’login’, $route);
http://domain.com/login ==>AuthController::loginAction()
2.4 Zend_Controller_Router_Route_Regex
$route = new Zend_Controller_Router_Route_Regex(
‘archive/(\d+)’,
array(
‘controller’ => ‘archive’,
‘action’ => ’show’
)
);
$router->addRoute(’archive’, $route);
例子: http://domain.com/archive/2006==>
$values = array(
1 => ‘2006′,
‘controller’ => ‘archive’,
‘action’ => ’show’
);

0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.