什么是 Route,在哪里以及如何定义它?

发布于 2024-11-30 02:39:59 字数 247 浏览 2 评论 0原文

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

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

,这会带来一个错误,指出“自定义”路线未定义。

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

I have several modules in my zend application. On one of the view script of my modules, I created a URL as such

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

This brings an error, saying Route "custom" is not define.

What is Route? Where to define it and How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄断弥空 2024-12-07 02:39:59

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

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);
    }

,因为我在我的事件中添加了自定义路由,每当我访问详细信息页面时,我都不必在我的网址中写入 id,所以我的网址将类似于

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

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

<一个href="http://localhost/example/events/detail/id/3" rel="nofollow">http://localhost/example/events/detail/id/3

In my bootstrap file i have initialized my routing by adding following function

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);
    }

as i have added the custom route in my events, commentaries whenever i visit detail page i dont have to write id in my url so my url will be like

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

If i wouldnt have added route than my url would be like

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

遗失的美好 2024-12-07 02:39:59

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

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

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

The Zend Framework manual has pretty decent documentation about routes and the router, including descriptions of several ways to define routes.

At a very basic level, routes are used both to parse URLs into parameters (like which controller and action should be used), and to do the reverse: take parameters and produce a URL.

For your purposes, unless you want to change how ZF will build your URL, you can just drop the "custom" part off of your url call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文