0

我的 zend 应用程序中有几个模块。在我的模块的一个视图脚本中,我创建了一个这样的 URL

$links['create'] = $this -> url(array("controller" => "roles", "action" => "create"), "custom");

这带来了一个错误,说 Route "custom" 没有定义。

什么是路线?在哪里定义它以及如何定义它?

4

2 回答 2

1

Zend Framework 手册有相当不错的关于路由和路由器的文档,包括定义路由的几种方法的描述。

在一个非常基本的级别上,路由既用于将 URL 解析为参数(例如应该使用哪个控制器和操作),也用于执行相反的操作:获取参数并生成 URL。

出于您的目的,除非您想更改ZF 构建 URL的方式url,否则您可以将“自定义”部分从调用中删除。

于 2011-08-17T01:55:09.907 回答
1

在我的引导文件中,我通过添加以下函数来初始化我的路由

public function _initRouting() {

        // Get Front Controller Instance
        $front = Zend_Controller_Front::getInstance();

        // Get Router
        $router = $front->getRouter();
        $routedetialevent = new Zend_Controller_Router_Route(
            '/events/detail/:id',
            array(
                'controller' => 'events',
                'action'     => 'detail'
            )
        );
        $routeregister = new Zend_Controller_Router_Route(
            '/index/register/:id',
            array(
                'controller' => 'index',
                'action'     => 'register'
            )
        );

        $routerdetail = new Zend_Controller_Router_Route(
            '/commentaries/details/:id',
            array(
                'controller' => 'commentaries',
                'action'     => 'details'
            )
        );


        $router->addRoute('post', $routedetialevent);
        $router->addRoute('register', $routeregister);
        $router->addRoute('detail', $routerdetail);
    }

因为我在我的活动中添加了自定义路线,所以每当我访问详细信息页面时,我都不必在我的 url 中写 id,所以我的 url 就像

http://localhost/example/events/detail/3

如果我不会添加路线,那么我的网址会像

http://localhost/example/events/detail/id/3

于 2011-08-17T06:54:32.467 回答