基于标准 PHP 查询字符串的路由

发布于 2024-08-24 05:38:14 字数 924 浏览 8 评论 0原文

如您所知,Zend Framework (v1.10) 使用基于斜杠分隔的参数的路由,例如。

[server]/controllerName/actionName/param1/value1/param2/value2/

Queston 是:如何强制 Zend Framework 使用标准 PHP 查询字符串检索操作和控制器名称,在本例中:

[server]?controller=controllerName&action=actionName&param1=value1&param2=value2

我已经尝试过:

protected function _initRequest()
{
    // Ensure the front controller is initialized
    $this->bootstrap('FrontController');

    // Retrieve the front controller from the bootstrap registry
    $front = $this->getResource('FrontController');

    $request = new Zend_Controller_Request_Http();
    $request->setControllerName($_GET['controller']);
    $request->setActionName($_GET['action']);
    $front->setRequest($request);

    // Ensure the request is stored in the bootstrap registry
    return $request;
}

但它对我不起作用。

As you know, Zend Framework (v1.10) uses routing based on slash separated params, ex.

[server]/controllerName/actionName/param1/value1/param2/value2/

Queston is: How to force Zend Framework, to retrive action and controller name using standard PHP query string, in this case:

[server]?controller=controllerName&action=actionName¶m1=value1¶m2=value2

I've tried:

protected function _initRequest()
{
    // Ensure the front controller is initialized
    $this->bootstrap('FrontController');

    // Retrieve the front controller from the bootstrap registry
    $front = $this->getResource('FrontController');

    $request = new Zend_Controller_Request_Http();
    $request->setControllerName($_GET['controller']);
    $request->setActionName($_GET['action']);
    $front->setRequest($request);

    // Ensure the request is stored in the bootstrap registry
    return $request;
}

But it doesn't worked for me.

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

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

发布评论

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

评论(2

国粹 2024-08-31 05:38:14
$front->setRequest($request);

该行仅设置 Request 对象实例。 frontController 仍然通过路由器运行请求,在路由器中它被分配要调用的控制器/操作。

您需要创建自己的路由器:

class My_Router implements Zend_Controller_Router_Interface
{
    public function route(Zend_Controller_Request_Abstract $request)
    {
        $controller = 'index';
        if(isset($_GET['controller'])) {
            $controller = $_GET['controller'];
        }

        $request->setControllerName($controller);

        $action = 'index';
        if(isset($_GET['action'])) {
            $action = $_GET['action'];
        }

        $request->setActionName($action);
    }
}}

然后在引导程序中:

protected function _initRouter()
{
    $this->bootstrap('frontController');
    $frontController = $this->getResource('frontController');

    $frontController->setRouter(new My_Router());
}
$front->setRequest($request);

The line only sets the Request object instance. The frontController still runs the request through a router where it gets assigned what controller / action to call.

You need to create your own router:

class My_Router implements Zend_Controller_Router_Interface
{
    public function route(Zend_Controller_Request_Abstract $request)
    {
        $controller = 'index';
        if(isset($_GET['controller'])) {
            $controller = $_GET['controller'];
        }

        $request->setControllerName($controller);

        $action = 'index';
        if(isset($_GET['action'])) {
            $action = $_GET['action'];
        }

        $request->setActionName($action);
    }
}}

Then in your bootstrap:

protected function _initRouter()
{
    $this->bootstrap('frontController');
    $frontController = $this->getResource('frontController');

    $frontController->setRouter(new My_Router());
}
这样的小城市 2024-08-31 05:38:14

您是否尝试过:$router->removeDefaultRoutes(),然后$request->getParams()$request->getServer()代码>?

Have you tried: $router->removeDefaultRoutes(), then $request->getParams() or $request->getServer()?

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