编写控制器/动作时 Zend_Navigation 与 Zend_Router 不兼容?

发布于 2024-10-17 05:00:14 字数 1242 浏览 6 评论 0原文

Zend 框架开发新手尝试找出 Zend 路由问题的简单解决方案。我确信你们中的一位专业人士可以伸出援手。

我有一个网站(使用 Zend_Navigation 在 Zend Framework 中构建),其中包含 75% 的静态 HTML 页面内容和一些控制器。顶级导航是在 Zend_Navigation 中构建的,循环遍历局部。

由于我的工作,我沿着这些思路构建了很多网站(包含大量静态页面),所以我想把它做好。我不想为每个静态页面(有很多)设置控制器和操作,我想创建一个解决方案,使用 Zend_Controller_Router_Route 自动路由所有静态内容到一个 StaticController ,其工作是根据 URL 中的控制器/动作配对包含或渲染 .phtml 页面,这些目录来自某种目录,例如 /layouts/staticpages

因为SEO 和各种原因我不想让这些静态页面的 URL 中的控制器配对显示为 /static/page/page1...它必须是“真实世界的描述” /section/page 的(例如 advantages/someadvantage

这是问题:当我设置正确的路线,但它把 Zend Navigation 弄乱了……我想是因为 Zend_Navigaion 不能很好地处理动态控制器/动作切换。

代码示例:

$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route('advantages/:page/*',
  array('controller' => 'static', 'action' => 'display', 'mode' => 'advantages',
    'page' => 'index'));
$router->addRoute('advantages', $route);

这很好地处理了“优点”部分中切换页面的工作,但是 Zend_Navigation 的自动控制器/动作编写以及“活动”节点的突出显示最终都搞砸了,因为它现在认为其控制器是“静态的” ”,动作是“显示”。

Zend_Navigation 与 Zend_Controller_Router_Route 根本不兼容吗?有没有更好的方法来执行这个单一静态页面控制器或全面处理静态内容?

Novice Zend Framework developer here trying to figure out a simple solution to a Zend Routing problem. I'm sure one of you pros can lend a hand.

I have a website (built in Zend Framework using Zend_Navigation) that contains 75% static HTML page content and a few controllers. Top level navigation is built in Zend_Navigation, looping through partials.

Because of my work I build a lot of sites along these lines (containing lots of static pages) so I want to get this right. I don't want to set up controllers and actions for each and every one of these static pages (there are many) and I wanted to create a solution where I used Zend_Controller_Router_Route to route all static content automatically through to a StaticController whose job it would be to include or render .phtml pages based on a controller/action pairing in the URL from some sort of directory like /layouts/staticpages

Because of SEO and various reasons I don't want to have the controller pairing in the URL for these static pages be visible as /static/page/page1... It has to be "real world descriptions" of the /section/page (eg. advantages/someadvantage )

Here is the problem: Using Zend_Controller_Router_Route can do the job when I set up the correct routes BUT it messes something awful with Zend Navigation... I assume because Zend_Navigaion doesn't play nice with on-the-fly controller/action switching.

Code example:

$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route('advantages/:page/*',
  array('controller' => 'static', 'action' => 'display', 'mode' => 'advantages',
    'page' => 'index'));
$router->addRoute('advantages', $route);

This handles the job of switching pages in the "advantages" section well enough, but Zend_Navigation's automatic controller/action writing AND the highlighting of "active" nodes ends up being all screwed up because it now thinks that its controller is "static" and action is "display".

Is Zend_Navigation fundamentally incompatible with Zend_Controller_Router_Route? Is there a better way of doing this single static page controller or handling static content across the board?

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

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

发布评论

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

评论(1

涙—继续流 2024-10-24 05:00:14

由于您对所有静态页面使用一个控制器/操作,因此您必须在显示 Zend Navigation 之前对其进行自定义。

查看Zend 文档中的示例 4

// the following route is added to the ZF router
Zend_Controller_Front::getInstance()->getRouter()->addRoute(
    'article_view', // route name
    new Zend_Controller_Router_Route(
        'a/:id',
        array(
            'module'     => 'news',
            'controller' => 'article',
            'action'     => 'view',
            'id'         => null
        )
    )
);

// a page is created with a 'route' option
$page = new Zend_Navigation_Page_Mvc(array(
    'label'      => 'A news article',
    'route'      => 'article_view',
    'module'     => 'news',    // required for isActive(), see note above
    'controller' => 'article', // required for isActive(), see note above
    'action'     => 'view',    // required for isActive(), see note above
    'params'     => array('id' => 42)
));

// returns: /a/42
$page->getHref();

Since you are using one controller/action for all static pages, you must customize your Zend Navigation before displaying it.

Check Example 4 in the Zend Documentation.

// the following route is added to the ZF router
Zend_Controller_Front::getInstance()->getRouter()->addRoute(
    'article_view', // route name
    new Zend_Controller_Router_Route(
        'a/:id',
        array(
            'module'     => 'news',
            'controller' => 'article',
            'action'     => 'view',
            'id'         => null
        )
    )
);

// a page is created with a 'route' option
$page = new Zend_Navigation_Page_Mvc(array(
    'label'      => 'A news article',
    'route'      => 'article_view',
    'module'     => 'news',    // required for isActive(), see note above
    'controller' => 'article', // required for isActive(), see note above
    'action'     => 'view',    // required for isActive(), see note above
    'params'     => array('id' => 42)
));

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