使用MVC在Zend Framework中创建非常简单的命名路由

时间:2021-09-14 11:10:55

I've just put together a very basic site using the Zend Framework and its MVC. (Actually, I'm not even using models at the moment, it's all just Controllers/Views for static info so far).

我刚刚使用Zend Framework及其MVC组建了一个非常基本的站点。 (实际上,我现在甚至不使用模型,到目前为止,它只是静态信息的控制器/视图)。

When I started toying with the Forms I realized that in the examples for Zend_Form they use something like this this set the form's action:

当我开始使用Forms时,我意识到在Zend_Form的示例中,他们使用类似这样的东西来设置表单的动作:

$form->setAction('/user/login')

Which contains the URL. I understand that Zend Framework has Routes and that they can be named, but I can't seem to grasp from the manual how to create a simple route for certain Controller/Actions so that I could do something like this:

其中包含URL。我知道Zend Framework有路由,并且它们可以被命名,但我似乎无法从手册中了解如何为某些Controller / Actions创建一个简单的路由,以便我可以这样做:

$form->setAction($named_route)
// or 
$form->setAction('named_route')

I hope my question is clear. I wasn't able to locate any duplicate questions, but if you spot one and point it out I won't mind.

我希望我的问题很清楚。我无法找到任何重复的问题,但如果你发现一个并指出它我不会介意。

Links to resources are as good as examples, so don't waste your time if you know of a decent blog post somewhere. Thanks!

资源链接和示例一样好,所以如果你知道某个地方有一个像样的博客文章,不要浪费你的时间。谢谢!

1 个解决方案

#1


References: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard - Look for "12.5.7.1. Zend_Controller_Router_Route" for a clearer explanation.

参考文献:http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard - 查找“12.5.7.1.Zend_Controller_Router_Route”以获得更清晰的解释。

This is not the best way to do it, but I have a working example. I welcome corrections. After seeing Shorten Zend Framework Route Definitions, I agree that named Routes should go in their own config (I use Django, and named Views/URLs are generally separated) - but here I'm just going to define Routes in my Bootstrap.

这不是最好的方法,但我有一个有效的例子。我欢迎更正。在看到Shorten Zend框架路由定义之后,我同意命名的路由应该在他们自己的配置中(我使用Django,并且命名的视图/ URL通常是分开的) - 但是在这里我只是要在我的Bootstrap中定义路由。

So, in Bootstrap.php, inside the Bootstrap Class of course, I've created an function that will be automatically run, like so:

所以,在Bootstrap.php中,当然在Bootstrap类中,我创建了一个自动运行的函数,如下所示:

public function _initRoutes()
{
    $frontController = Zend_Controller_Front::getInstance();
    $route = new Zend_Controller_Router_Route(
        'login/', // The URL, after the baseUrl, with no params.
        array(
            'controller' => 'login', // The controller to point to.
            'action'     => 'index'  // The action to point to, in said Controller.
        )
    );
    $frontController->getRouter()->addRoute('loginpage', $route);
}

In the above example, "loginpage" will be the Name of the "Named Route".

在上面的例子中,“loginpage”将是“命名路由”的名称。

So, inside my LoginController, (in a function that builds the form) instead of doing

所以,在我的LoginController中,(在构建表单的函数中)而不是在做

$form->setAction('/blah/login')

I retrieve the URL of the named Route and pass that in, like so:

我检索指定Route的URL并将其传入,如下所示:

$form_action_url = $this->view->Url(array(), 'loginpage', true);
// -- SNIP --
$form->setAction($form_action_url) // ...

This may be pointless and wrong, but it seems to work at the moment. My reason for wanting a named URL, when Zend Framework handles URLs as /Controller/View(Action)/ automatically is because I'm anal about that kind of thing. I've been using Django for awhile, where the URLs are predefined, and I like it that way.

这可能毫无意义,也可能是错误的,但目前似乎有效。当Zend Framework自动将URL作为/ Controller / View(Action)/处理时,我想要一个命名URL的原因是因为我对这种事情嗤之以鼻。我一直在使用Django,其中URL是预定义的,我喜欢这样。

The Zend Framework MVC urls working out of the box is nice, tho.

Zend Framework MVC网址开箱即用,很好。

Feel free to add notes and corrections to how this should work!

随意添加注释和更正这应该如何工作!

#1


References: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard - Look for "12.5.7.1. Zend_Controller_Router_Route" for a clearer explanation.

参考文献:http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard - 查找“12.5.7.1.Zend_Controller_Router_Route”以获得更清晰的解释。

This is not the best way to do it, but I have a working example. I welcome corrections. After seeing Shorten Zend Framework Route Definitions, I agree that named Routes should go in their own config (I use Django, and named Views/URLs are generally separated) - but here I'm just going to define Routes in my Bootstrap.

这不是最好的方法,但我有一个有效的例子。我欢迎更正。在看到Shorten Zend框架路由定义之后,我同意命名的路由应该在他们自己的配置中(我使用Django,并且命名的视图/ URL通常是分开的) - 但是在这里我只是要在我的Bootstrap中定义路由。

So, in Bootstrap.php, inside the Bootstrap Class of course, I've created an function that will be automatically run, like so:

所以,在Bootstrap.php中,当然在Bootstrap类中,我创建了一个自动运行的函数,如下所示:

public function _initRoutes()
{
    $frontController = Zend_Controller_Front::getInstance();
    $route = new Zend_Controller_Router_Route(
        'login/', // The URL, after the baseUrl, with no params.
        array(
            'controller' => 'login', // The controller to point to.
            'action'     => 'index'  // The action to point to, in said Controller.
        )
    );
    $frontController->getRouter()->addRoute('loginpage', $route);
}

In the above example, "loginpage" will be the Name of the "Named Route".

在上面的例子中,“loginpage”将是“命名路由”的名称。

So, inside my LoginController, (in a function that builds the form) instead of doing

所以,在我的LoginController中,(在构建表单的函数中)而不是在做

$form->setAction('/blah/login')

I retrieve the URL of the named Route and pass that in, like so:

我检索指定Route的URL并将其传入,如下所示:

$form_action_url = $this->view->Url(array(), 'loginpage', true);
// -- SNIP --
$form->setAction($form_action_url) // ...

This may be pointless and wrong, but it seems to work at the moment. My reason for wanting a named URL, when Zend Framework handles URLs as /Controller/View(Action)/ automatically is because I'm anal about that kind of thing. I've been using Django for awhile, where the URLs are predefined, and I like it that way.

这可能毫无意义,也可能是错误的,但目前似乎有效。当Zend Framework自动将URL作为/ Controller / View(Action)/处理时,我想要一个命名URL的原因是因为我对这种事情嗤之以鼻。我一直在使用Django,其中URL是预定义的,我喜欢这样。

The Zend Framework MVC urls working out of the box is nice, tho.

Zend Framework MVC网址开箱即用,很好。

Feel free to add notes and corrections to how this should work!

随意添加注释和更正这应该如何工作!