dongqiao6445 2014-12-21 22:39
浏览 60
已采纳

控制器加载不同的视图(URL /路由?)

The issue/brief intro

What I'm currently in the process of making is a simple register/login-system (which will obviously expand into more later on). I've got a controller named Users, which features mainly two functions. One is show_register() and one is send_register(). This is paired with a model User_Model, a so called Factory UserFactory and currently (mainly for testing, these will later be combined) three views register_success, register_fail and show_register. They are basically one view for saying that "Hey, congrats, you managed to register", one for saying "Oh noes, no register!" and one for showing the actual registration form.

Upon heading onto http://url.com/register, you are automatically shown the registration form itself (done through routing with $route['register'] = 'users/show_register';).

So, once you fill out the form in the show_register, the form action leads to the send_register function in the Users-controller. Input is checked, and if all is valid, it is sent to the UsersFactory for database input. If all is well, the controller receives "true" back as a result, and shows the corresponding view. This is where our problem kicks in!

If I manage to register, my URL is now: http://url.com/public/users/send_register. The public-folder is where I keep the files, and I've realized that needs to be removed from the URL through htaccess, which I'll find my way about how to do. The issue is that I don't wish my URL to be consisting of the users-controller and the send_register function of that. I wish it to remain as http://url.com/register/, but show a different view.

What I've tried/looked into

I've looked into routing first of all. I realize (hopefully I'm right about this, otherwise I need to learn how to read) that it won't work for the issue I've described. The main reason for that is because it takes whatever I specify and sends that to a specific class/function. In this case, what we're doing is already going to the right class/function, but the "wrong" URL is shown to the user. It's also difficult since I've got different methods inside of this controller, meaning that I can't statically map all actions to lead to a single function.

I've also looked into .htaccess, but I have not found a way to actually write it. For good measure, I'll have my .htaccess file printed here on the bottom in case it would help. All I've done is modify so that the index.php-file is removed from the URL.

.htaccess file

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

If there are any other files you'd like me to show, I'll gladly do so. For now, all I can think of that could be relevant is the controller itself.

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

    public function show_register()
    {
        $this->load->view("show_register");
    }

    public function send_register()
    {
        // Populate the model with the information we got from the form.
        $data = array(
            'user_name' => $this->input->post('username'),
            'user_pass' => $this->input->post('password'),
            'user_email' => $this->input->post('email'),
            'user_level' => 1
            );

        // Then pass that on to the User Factory, which will insert that into the database.
         $this->load->library("UserFactory");

         if($this->userfactory->registerUser($data))
         {
            $this->load->view("register_success");
         }
         else
         {
            $this->load->view("register_fail");
         }
    }
}
?>
  • 写回答

1条回答 默认 最新

  • dongweihuan8610 2014-12-22 01:32
    关注

    Apologies for it taking so long for an answer to be given!

    Why not just do an ajax call?

    On your view where the form is:

    <div id="message_box" class="hidden"></div>
    <form  id="theForm" method="post" action="submitForm()"></form>
    
    <script>
    function submitForm() {
        $.post("<?= site_url('Users/send_register');?>", $("#theForm").serialize(), function(data) {
            success: $("#message_box").html(data).show();
            error: $("#message_box").html("<p>There was an error with your request</p>").show();
        });
    }
    </script>
    

    There are of course a million different ways to do this. But I'm assuming those views are just message views, as in just short html success/failures (as you'd stated above). The advantage of ajax would be that the user would stay on the page, and you'd hide your url.

    Hope that helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?