基于标准 PHP 查询字符串的路由 [英] Routing based on standard PHP query string

查看:27
本文介绍了基于标准 PHP 查询字符串的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

但它对我不起作用.

推荐答案

$front->setRequest($request);

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

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

这篇关于基于标准 PHP 查询字符串的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆