ZF2 通过 post vars 路由 [英] ZF2 routing via post vars

查看:60
本文介绍了ZF2 通过 post vars 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过处理 http post vars 来路由请求,例如通过将模块"、控制器"、动作"等放在表单中,将表单的 action="..." 目标设置为应用程序的默认路由,然后从那里路由到模块/控制器/动作路线.URL/module/controller/action 不能访问到 module/controller/action 的路由,所以问题是,如果路由是在 module.config.php 中配置的,那么它们也可以通过 URL 访问?我可能在那里遗漏了一点,它可能简单明了,如果我知道 module.config.php 中路由的正确配置,或者是否有必要设置自己的自定义路由服务?

I want to route requests by processing http post vars, e.g. by putting and the like for "module", "controller", "action", etc., in a form, setting the action="..." target of the form to the application's default route and from there route to the module/controller/action route. The routes to module/controller/action shall not be accessible by the URL /module/controller/action, so the problem is, if the routes are configured in module.config.php, then they become accessible through the URL also? I may be missing a point there, it might be straightforward and simple, if only I knew the proper configuration for the routing in module.config.php, or is it necessary to set up an own custom routing service?

重定向到路由会在浏览器的地址栏中显示路由 URL,这是我想要避免的,但不知道如何.

The redirect to route would reveal the route URL in the browser's address bar, and that is what I want to avoid, and don't know how.

if ($this->request->isPost()) {
    $post = $this->request->getPost();
    if (isset($post->module) && isset($post->controller) && isset($post->action)) {
        return $this->redirect()->toRoute($post->module, array(
            'controller' => $post->controller,
            'action' =>  $post->action
        ));
    } 
}

找到一半解决方案:使用\Mvc\Controller\Plugin\Forward,像这样:

Found half a solution: Use \Mvc\Controller\Plugin\Forward, like this:

在应用程序的默认控制器/操作中,通过在 module.config.php 的 invokables 部分中定义的名称调用控制器,例如对于名为注册"的控制器:

in the application's default controller/action, calling a controller by its name as defined in the invokables section in the module.config.php, e.g. for a controller named 'register':

if ($this->request->isPost()) {
    $post = $this->request->getPost();
    if (isset($post->module) && isset($post->controller) && isset($post->action)) {
        return $this->forward()->dispatch('register', array('action' => $post->action));
    } 
}

现在我需要从模块/控制器名称中获取控制器可调用名称,就像来自后变量一样.怎么样?

Now I need to get the controller invokable name from the module/controller name as from the post vars. How?

推荐答案

解决方案的关键是在模块的 module.config.php 中定义唯一的控制器可调用名称,以便控制器可调用名称可以在一种结构化定义的方式,例如对可调用对象使用语法MyModule \ Controller \ MyController".

The key to the solution is to define unique controller invokable names in the modules' module.config.php's in a way that the controller invokable names can be constructed in a structured defined way, for example use the syntax "MyModule \ Controller \ MyController" for the invokables.

示例应用:

模块/控制器:

  • 申请
    • 索引
    • 索引
    • 登录
    • 注册

    文件module/Application/config/module.config.php中的代码:(注意:应用模块的路由仍然存在,所以我们仍然可以有一些可以通过 URL 路由访问的页面,即我们希望搜索引擎访问的页面.如果我们也想摆脱这些,那么我们必须将路由设置为键入主机名",如 zf2 网站只有一个入口点,URL 中没有路由/路径.)

    Code in file module/Application/config/module.config.php: (NB: Routes for application module still exist, so we can still have some pages that can be reached through URL routing, i.e. pages that we want to be reachable by search engines. If we want to get rid of these, too, then we have to set routing to type 'Hostname', as described in zf2 website with a single entry-point, no routes/paths in URL.)

    return array(
        'controllers' => array(
            'invokables' => array(
                'Application\Controller\Index' => 'Application\Controller\IndexController',
            ),
        ),
    
        'router' => array(
            'routes' => array(
                'home' => array(
                    'type' => 'Zend\Mvc\Router\Http\Literal',
                    'options' => array(
                        'route'    => '/',
                        'defaults' => array(
                            '__NAMESPACE__' => 'Application\Controller',
                            'controller'    => 'Index',
                            'action'        => 'index',
                        ),
                    ),
                ),
                'application' => array(
                    'type'    => 'Literal',
                    'options' => array(
                        'route'    => '/application',
                        'defaults' => array(
                            '__NAMESPACE__' => 'Application\Controller',
                            'controller'    => 'Index',
                            'action'        => 'index',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'default' => array(
                            'type'    => 'Segment',
                            'options' => array(
                                'route'    => '/[:controller[/:action]]',
                                'constraints' => array(
                                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                    'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                                ),
                                'defaults' => array(
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    

    如果我们也想在模块Application中抑制URL路由,那么文件module/Application/config/module.config.php中的路由应该这样定义:

    If we want to suppress URL routing in module Application, too, then the routes in file module/Application/config/module.config.php should be defined as such:

    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Hostname',
                'options' => array(
                    'route'    => '4yougroup.local.f4u.0101',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
            ),
        ),
    ),
    

    文件module/User/config/module.config.php中的代码:(注意:这里没有定义路由!)

    Code in file module/User/config/module.config.php: (NB: No routes defined here!)

    return array(
        'controllers' => array(
            'invokables' => array(
                'User\Controller\Index'     => 'User\Controller\IndexController',
                'User\Controller\Register'  => 'User\Controller\RegisterController',
                'User\Controller\Login'     => 'User\Controller\LoginController',
            ),
        ),
    

    为了使用 HTTP POST 变量通过应用程序的默认路由路由所有内容,将此代码放入应用程序模块的 IndexController indexAction 函数中:

    In order to route everything through the application's default route by using HTTP POST vars, put this code in the Application module's IndexController indexAction function:

    class IndexController extends AbstractActionController
    {
        public function indexAction()
        {
            if ($this->request->isPost()) {
                $post = $this->request->getPost();
                if (isset($post->module) && isset($post->controller) && isset($post->action)) {
                    $controllerName = ucfirst($post->module) . "\\Controller\\" . ucfirst($post->controller);  
    
                    return $this->forward()->dispatch($controllerName, array('action' =>  $post->action));
                } 
            }
        }
    }
    

    然后把这样的 HTML 代码放在一个视图中:

    Then put HTML code like this in a view:

    <form class="form-horizontal" action="" method="POST">
        <input type="hidden" name="module" value="user">
        <input type="hidden" name="controller" value="register">
        <input type="hidden" name="action" value="index">
    

    ...然后瞧...

    如果有人能提出更精简、更清洁的解决方案,那就太好了:)

    If anyone could suggest a leaner and cleaner solution, would be nice :)

    这篇关于ZF2 通过 post vars 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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