English 中文(简体)
如何在Zend Framework中创建自定义路由器?
原标题:How to create a custom router in Zend-Framework?

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

mytutorialsite.com/category/:类别名称

# 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

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

- html
- css
- js
- php

这意味着,存在以下页面:

  • mytutorialsite.com/category/html
  • mytutorialsite.com/category/css
  • mytutorialsite.com/category/js
  • mytutorialsite.com/category/php

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

  • mytutorialsite.com/category/foo

您应该得到一条404页面不存在消息。

我该如何做到这一点?

最佳回答

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

第二个选项是捕获所有类别/*路线,并在操作中检查该类别是否存在。

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

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中:

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

现在,来自category/SOMETHING的所有请求都将转到默认模块,即类别控制器。调度程序检查操作SOMETHING是否存在。当它这样做时,它执行该操作。否则,将抛出Zend_Controller_Action_Exception(“操作不存在”)。

总之,这两种方法都有效。有了第一个,你就有了更多的控制权。第二种方法更简单,但当类别Controller中存在editAction、addAction或removeAction时,它们也可以被触发(所以要小心该方法)。

PS。当然,routeStartup函数应该有一个缓存机制,以防止对每个请求进行数据库查询。

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签