提交表单后,从控制器发送数据以进行查看的最佳方法? [英] Best way to send data from controller to view after a form is submitted?

查看:63
本文介绍了提交表单后,从控制器发送数据以进行查看的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC架构开发一个基本的Web应用程序.我正在建立自己的系统,以尝试并全面了解MVC架构的工作原理,因此它是学习活动的两倍.

I'm developing a basic web app using the MVC architecture. I'm building my own to try and fully understand how the MVC arch works so this doubles as a learning exercise.

我正在使用Aura Router类将URL映射到控制器和操作,以便将mysite.com/login之类的内容映射到LoginController,如果我向mysite.com/login/login提交一个表单,它将对其进行映射.到LoginController-> doAction('login').

I am using the Aura Router classes to map my URLs to controllers and action so that something like mysite.com/login will be mapped to LoginController and if I submit a form to mysite.com/login/login it will map it to LoginController->doAction('login').

控制器示例如下:

class LoginController implements iController {

    public function doAction( PDO $dbh, $action ) {

        switch( $action ) {
            case 'login':
                //login here
                $user = new User();
                $user_id = FALSE;

                if( $user_id = $user->login( $_POST['email'], $_POST['password'] ) ) {
                    //save user id to session
                }
                else {
                    $results = array( 'errors' => array( 'invalid' ) );
                    MembershipFunc::redirect( '/login', $results );
                }

                break;
            case 'logout':
                //logout
                break;
            default:
                break;
        }

    }
}

我面临的问题是,避免人们刷新页面并重新提交数据,如果登录失败,我希望将用户转发回登录页面.目前,如果我不转发它们,那么它们将出现在页面mysite.com/login/login上,我认为它不是特别干净.对于我的简单登录表单来说还可以,因为错误可以重定向到mysite.com/login?error=email,password&email=user@domain.com,但如果表单很大,那么我将得到一个庞大的URL查询真的很毛.

The problem I am facing is that to avoid people from refreshing the page and resubmitting the data I like to forward the user back to the login page if the login failed. Currently if I don't forward them then they would appear on the page mysite.com/login/login and I don't think it's particularly clean. It's okay for my simple login form because an error could redirect to mysite.com/login?error=email,password&email=user@domain.com but in the case of a huge form then I would get a huge URL query which is really gross.

我一直在寻找有关MVC工作原理的基本(至今有用)PHP代码示例,以寻找好的资源,而我一直在努力寻找任何特别有用的东西.我应该避免将URL结构映射到操作,而是选择放置一个名为"action"的POST字段吗?我是否应该以某种方式构建一个系统,以使该数据通过SESSION数据中的数组传递?

I have scoured for good resources with basic (yet useful) PHP code examples on how MVC works and I've struggled to find anything particularly useful. Should I avoid mapping my URL structures to an action and instead opt for putting a POST field called "action" instead? Should I somehow build a system where I pass this data through an array in the SESSION data?

有什么建议吗?

注意:我只想指出我知道这个问题不适合SO文化,因为它可能没有正确/错误的答案,但我发现SO始终拥有最好/最博学的用户-根据.如果有人对我应该将这些问题指向什么地方有提示,我将不胜感激,这样我就不会太混乱了!

推荐答案

在我的MVC中,URL看起来像: index.php?c = Controller& m = ControllerMethod& d =斜杠/分隔/列表/内容/内容

In my MVC URLs look like: index.php?c=Controller&m=ControllerMethod&d=slash/sepparated/list/of/stuff

数据(d =)在斜杠上分解,并作为数组传递给每个控制器方法. 自动加载(通过spl_autoload_register())用于调用类(c =),然后调用该类中的方法(m =).

Data (d=) is exploded on the slashes and passed as an array to every controller method. Autoloading (via spl_autoload_register()) is used to call the class (c=) and then the method in that class (m=).

另外,听起来您不是在表单上未设置ACTION,还是故意将ACTION设置为GET.通常,ACTION应该是POST,以使URL保持健全.除了搜索表单.这些可以是具有各种优点的GET.

Also, it sounds like you're either not setting the ACTION on your form or you're deliberately setting the ACTION to GET. As a general rule, ACTION should be POST to keep the URLs sane. Except search forms. Those can be GET with various advantages.

重定向以防止刷新时重新提交表单是您的最佳选择(可能是唯一的选择).但是在我的MVC index中,php?c = user& m = login同时处理登录页面和登录操作.

Re-directing to prevent a resubmitted form on refresh is your best option (probably only option). But in my MVC index.php?c=user&m=login handles both the login page and the login action.

示例

class login extends Controller {

    public function login($data) {

        if(empty($_POST)) {

            $this->view = "login.tpl";

            return TRUE;

        }


        $res = $this->model->auth();

        if($res !== TRUE) {

            $_POST = NULL;

            $this->errorState = 1;
            $this->errorMsg = "Invalid login details";

            $this->login();

            return FALSE;

        }

        Core::setMessage('success', 'user', 'login', '2', 'Logged in successfully');

        $home = new home(); //whatever the main controller is
        $home->index($data);

        //alternatively you can redirect

        header("Location: index.php?c=home&m=index);

        return TRUE;

    }

}

这有意义还是我完全错过了标记?

Does this make sense or have I completely missed the mark?

这篇关于提交表单后,从控制器发送数据以进行查看的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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