如何在 Zend-Framework 中创建自定义路由器? [英] How to create a custom router in Zend-Framework?

查看:40
本文介绍了如何在 Zend-Framework 中创建自定义路由器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义路由器来启用以下页面:

I am using a custom Router to enable pages like:

mytu​​torialsite.com/category/:categoryname

# added to application.ini
resources.router.routes.categorynameOnCategory.route = /category/:categoryname
resources.router.routes.categorynameOnCategory.defaults.module = default
resources.router.routes.categorynameOnCategory.defaults.controller = category
resources.router.routes.categorynameOnCategory.defaults.action = categoryname

我还有数据库表类别",其中存储了所有类别.例如,假设以下类别当前存储在我的数据库中:

I also have database table 'categories' in which all categories are stored. For example, let's say the following categories are currently stored in my database:

- html
- css
- js
- php

这意味着存在以下页面:

This means, the following pages exist:

  • mytu​​torialsite.com/category/html
  • mytu​​torialsite.com/category/css
  • mytu​​torialsite.com/category/js
  • mytu​​torialsite.com/category/php

但是当您访问一个类别名称未在数据库中列出的页面时,例如:

But when you visit a page with a categoryname that is not listed in the database, like:

  • mytu​​torialsite.com/category/foo

您应该会收到 404 页面不存在消息.

我如何做到这一点?

推荐答案

我认为你的意思是在你的路由中使用 categoryname 作为动作, :categoryname 应该被用作动作?您可以使用两种方法.首先是您只将路由添加到存在类别的路由器.当请求 category/foo 时,路由器将无法识别路由并抛出 404 错误.

I think you mean with categoryname as action in your route the :categoryname should be used as an action? There are two methods you can use. First is you add only the routes to the router where categories exist. When category/foo is requested the router won't recognize the route and throw the 404 error.

第二个选项是您捕获所有 category/* 路由,并在您的操作中检查该类别是否存在.

The second option is you catch all category/* routes and inside your action you check if the category exists.

对于第一个选项,添加一个带有 routeStartup 函数的 frontController 插件.在这个钩子中你可以做:

For the first option, add a frontController plugin with a routeStartup function. In this hook you can do:

public function routeStartup(Zend_Controller_Request_Abstract $request)
{
    // Get the router
    $router     = Zend_Controller_Front::getInstance()->getRouter();

    // Fetch all your categories
    $category   = new Application_Model_Category;
    $categories = $category->fetchAll();

    // Loop and add all individual categories as routes
    foreach ($categories as $category) {
        $route  = 'category/:' . $category->name;
        $params = array(
            'module'     => 'default',
            'controller' => 'category',
            'action'     => $category->name
        );

        $route = new Zend_Controller_Router_Route($route, $params);
        $router->addRoute($category->name, $route);
    }
}

另一种方法对于路由来说更简单.在您的 application.ini 中:

The other method is more simple for the route. In your application.ini:

resources.router.routes.category.route      = "category/:action"
resources.router.routes.category.module     = "default"
resources.router.routes.category.controller = "category"

现在来自类别/某些东西的所有请求都将转到默认模块类别控制器.调度程序检查操作 SOMETHING 是否存在.当它这样做时,它执行操作.如果不是,则抛出 Zend_Controller_Action_Exception(动作不存在").

Now all requests from category/SOMETHING will go to the default module, category controller. The dispatcher checks if the action SOMETHING exists. When it does, it executes the action. When not, a Zend_Controller_Action_Exception ("action does not exist") is throw.

简而言之,这两种方法都有效.有了第一个,您将获得更多控制权.第二个更简单,但是当例如categoryController 中存在editAction、addAction 或removeAction,它们也可以被触发(所以要小心使用该方法).

So in short, both methods work. With the first you get more control. The second is more simple but when e.g. an editAction, addAction or removeAction in the categoryController exist, they can be triggered as well (so be careful with that method).

附注.当然,routeStartup 函数应该有缓存机制,防止每次请求都进行数据库查询.

PS. Of course, the routeStartup function should have a caching mechanism to prevent a database query on each request.

这篇关于如何在 Zend-Framework 中创建自定义路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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