Zend Framework:如何映射完全动态的路线 [英] Zend Framework: How to map fully dynamic route

查看:80
本文介绍了Zend Framework:如何映射完全动态的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的标题很难想出,反正可能不是最好的。



我有一个包含地区,类别和供应商的网站,显而易见的解决方案是使用默认路由

  /:module /:controller /:action 

这样我的URL看起来像这样

  /地区/中部地区/类别/时尚 
/地区/中部地区/供应商/特德贝克

我想要实现的是这样的URL格式,但是,这需要涉及数据库查询以检查 midlands 时尚特德贝克

  /中部地区/时尚 
/中部地区/特德贝克

我最初的解决方案是使用这样的东西

  / region / midlands / fashion 

路线定义为

 路线s.category.route = / region /:region /:category 
route.category.defaults.controller =类别
route.category.defaults.action =索引
route.category。 defaults.module =默认
route.category.defaults.category =否
route.category.defaults.region =否

route.supplier.route = / supplier /:供应商
route.supplier.defaults.controller =供应商
route.supplier.defaults.action =索引
route.supplier.defaults.module =默认
route.supplier.defaults .supplier = false

但这意味着所有内容都以 region 供应商。我几乎需要通过插件完全劫持该请求?



实现此目标的最佳方法是什么?



感谢您的帮助。



编辑。



@ St.Woland ,问题是我要这条路线

  /:region /:supplier 

要使用此URL

  / midlands / ted-baker 

但是该路由有效地覆盖了默认路由器

ErrorController
来完成此任务。我在有控制器或没有路由的地方捕获到异常,然后采取相应的措施。



有一些针对特定路由的数据库表的调用,找不到该表而不是按照St.Woland的解决方案获取所有内容。结果将使用标记进行缓存,这很有帮助,这会删除所有用于查找路线的数据库查询

 公共函数errorAction()
{
$ errors = $ this-> _getParam('error_handler');

开关($ errors-> type)
{
case Zend_Controller_Plugin_ErrorHandler :: EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler :: EXCEPTION_NO_CONTROLLER:
//代码用于在Db中找到路线的位置
}
}


The title of this question was hard to come up with, it might not be the best, anyway.

I have a site with regions, categories and suppliers, the obvious solution is to use the default route of

"/:module/:controller/:action"

So that my URLs will look something like this

"/region/midlands/category/fashion"
"/region/midlands/supplier/ted-baker"

What I want to achieve is a URL format like this however, this would need to involve a database query to check for the existence of midlands, fashion and ted-baker

"/midlands/fashion"
"/midlands/ted-baker"

My original solution was to use something like this

"/region/midlands/fashion"

With a route defined as

routes.category.route = "/region/:region/:category"
routes.category.defaults.controller = category
routes.category.defaults.action = index
routes.category.defaults.module = default
routes.category.defaults.category = false
routes.category.defaults.region = false

routes.supplier.route = "/supplier/:supplier"
routes.supplier.defaults.controller = supplier
routes.supplier.defaults.action = index
routes.supplier.defaults.module = default
routes.supplier.defaults.supplier = false

But that means prefixing everything with region or supplier. I almost need to hijack the request completely with a plug in?

What is the best way of achieving this?

Thanks for any help.

Edit.

@St.Woland, the problem is that I want this route

/:region/:supplier

To work with this URL

/midlands/ted-baker

But that route effectively overrides the default router

解决方案

I have used the ErrorController to do this in the end. I catch the Exception where there is controller or no route and then act accordingly.

There are calls made to a few database tables for the specific route which cannot be found rather than fetching all as in St.Woland's solution. The results are cached with tags which helps a great deal, this removes all database queries for finding routes

public function errorAction()
{
    $errors = $this->_getParam('error_handler');

    switch ($errors->type) 
    {
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            //Code for locating routes in Db goes here
    }
}

这篇关于Zend Framework:如何映射完全动态的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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