Zend Framework 中数据库驱动路由的教程?

发布于 2024-07-09 21:30:03 字数 319 浏览 5 评论 0原文

我正在开发一个需要使用数据库驱动的 MVC 方案的项目,其中控制器和视图的路由通过单个数据库表进行控制。 但是,我无法找到任何使用当前版本的框架演示这一点的教程(它们似乎都是在几个版本之前编写的),我想知道是否有人用更新的版本做过类似的事情框架的介绍,或者是否有人知道讨论如何以简单的方式完成此任务的博客或教程。

基本思想是,将有一个 sitePage 表,其中包含 pageName、控制器、模块和视图字段。 处理请求时,我需要在数据库中查询给定的 pageName 并确定适当的控制器、模块和视图,然后将其传递到必要的 Zend 类中以继续正常路由和处理请求。

提前致谢。

I am working on a project that needs to use a database driven MVC scheme where the route to the controllers and views are controlled through a single database table. However, I haven't been able to find any tutorials that demonstrate this with a current version of the framework (they all appear to have been written several versions ago) and I was wondering if anyone has done something like this with a more recent version of the framework or if anyone knows of blogs or tutorials that discuss how to accomplish this in a simple manner.

The basic idea is that there will be a sitePage table that will contain pageName, controller, module and view fields. When the request is processed I need to query the database for the given pageName and determine the appropriate controller, module and view and then pass this into the necessary Zend class to continue with the normal routing and processing of the request.

Thanks in advance.

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

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

发布评论

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

评论(3

[浮城] 2024-07-16 21:30:03

您还可以在插件中使用routeStartup()方法。
例如:

 class My_Plugin_PageRoute extends Zend_Controller_Plugin_Abstract {

     public function routeStartup ()  {
         $front = Zend_Controller_Front::getInstance();
                    $pages = new Model_Pages();
                    $page_data = $pages ->getPageInfo();

        $router = $front->getRouter();

        foreach($page_data as $page) {
            $r = new Zend_Controller_Router_Route(
                '' . $page -> page_name,
                array('controller' => 'pages',
                      'action' => 'index',
                      'page_id' => $page -> page_id)
            );
            $router->addRoute('pages_' . $page -> page_id, $r);
        }

     }

 }

You can also use the routeStartup() method in your plugin.
eg:

 class My_Plugin_PageRoute extends Zend_Controller_Plugin_Abstract {

     public function routeStartup ()  {
         $front = Zend_Controller_Front::getInstance();
                    $pages = new Model_Pages();
                    $page_data = $pages ->getPageInfo();

        $router = $front->getRouter();

        foreach($page_data as $page) {
            $r = new Zend_Controller_Router_Route(
                '' . $page -> page_name,
                array('controller' => 'pages',
                      'action' => 'index',
                      'page_id' => $page -> page_id)
            );
            $router->addRoute('pages_' . $page -> page_id, $r);
        }

     }

 }
书信已泛黄 2024-07-16 21:30:03

我意识到一种更优雅的方法确实是使用路由器,但为此您需要通过扩展 Zend_Controller_Router_Abstract 类并实现“route”方法来创建自定义路由器。

您将获得一个 Zend_Controller_Request_Abstract 对象作为“route”方法的参数。 在那里您可以与数据库对话,然后您可以使用:

Zend_Controller_Request_Abstract::setModuleName(),
Zend_Controller_Request_Abstract::setControllerName(),
Zend_Controller_Request_Abstract::setActionName()

来定义您的路线。

我希望它有帮助!

I realized that a more elegant approach is indeed to use a router, but for that you would need to create a custom one by extending the Zend_Controller_Router_Abstract class and implementing the "route" method.

You get a Zend_Controller_Request_Abstract object as the parameter of the "route" method. There you can talk to the database and then you can use:

Zend_Controller_Request_Abstract::setModuleName(),
Zend_Controller_Request_Abstract::setControllerName(),
Zend_Controller_Request_Abstract::setActionName()

to define your route.

I hope it helps!

夏九 2024-07-16 21:30:03

也许最好的方法不是使用路由器,而是使用插件或通用控制器。 如果没有更深入的分析,我建议您创建一个前端控制器插件,然后在 preDispatch() 方法中您可以与数据库通信并重置请求,以便将其分派到正确的控制器。

您还可以通过使用通用控制器获得相同的效果,所有请求都路由到它,然后它可以在与数据库通信后转发到正确的控制器,尽管我更喜欢使用插件。

根据手册:

在调度程序调度操作之前调用 preDispatch()。 此回调允许代理或过滤器行为。 通过更改请求并重置其分派标志(通过 Zend_Controller_Request_Abstract::setDispatched(false)),可以跳过和/或替换当前操作。

http://framework.zend.com/manual/en/zend。控制器.plugins.html

Maybe the best aproach is not by using routers but by using plugins or a common controller. Without a deeper analysis I would suggest you to create a Front Controller Plugin, and then inside the preDispatch() method you can talk to the database and reset the request so it is dispatched to the right controller.

You can also get the same effect by using a common controller, all requests are routed to it then it can forwards to the right controller after talking to the database, although I prefer to use a plugin.

From the Manual:

preDispatch() is called before an action is dispatched by the dispatcher. This callback allows for proxy or filter behavior. By altering the request and resetting its dispatched flag (via Zend_Controller_Request_Abstract::setDispatched(false)), the current action may be skipped and/or replaced.

http://framework.zend.com/manual/en/zend.controller.plugins.html

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