如何在CodeIgniter中维护当前URL

3yhwsihp  于 2023-01-15  发布在  其他
关注(0)|答案(2)|浏览(169)

我又一次迷失了方向。我要从这件事开始,因为我甚至不确定我到底想要什么。
示例:我有一篇文章视图。URL是:http://index.php/news/article/3这实际上是在我的news控制器中运行article($id)函数,并将3作为参数,然后该函数获取文章信息并将其显示在视图中。
在文章页面上,用户也可以登录。登录是在我的表单form_open('core/login') ... button... </form>中按提交按钮触发的。在函数中,我登录用户,并根据登录的用户更改了一些元素来刷新当前视图。问题是,URL现在是http://index.php/core/login。显然,我希望它是原始的URL。
有什么可能简单的解决办法来实现这一点吗?感谢大家的阅读和提前回复。

aurhwmvo

aurhwmvo1#

没有更多的代码很难,但让我给予你我的理论/采取这一点:
默认控制器:
1.用户未登录-显示默认页眉、内容、页脚
1.用户按下登录,显示表格
1.用户已通过身份验证-是(继续)否(返回表单)
1.用户被重定向('home')'d
1.默认控制器/主控制器检查是否已授权:如果被授权,则显示已登录的页眉、内容和页脚,或者简单地将'loggedIN'作为$data ['loggedIN']变量传递给视图-但这打破了MVC框架的思想。
更多的信息,从你我可以更具体,或者我们可以谈谈IRC。
从我现在正在使用的控制器添加此代码-我使用ion_auth库(您应该查找它,它非常出色)。
这是我的默认控制器-正如您所看到的,一些简单的逻辑加载了不同的视图/状态。

public function index(){

        if ($this->data['auth_login'] ) {

            /*is already signed in so just present the lobby?*/
            $data['page_title'] = "HN Lobby";
            $data['menuItems'] = nav_anchor_helper_authd();
            $data['myUserID'] = $this->ion_auth->get_user_id();

            $data['lobby_players'] = $this->lobby_model->get_players();

            $this->load->view('template/public/header',$data);
            $this->load->view('player_pages/nav_2',$data);
            $this->load->view('player_pages/lobby',$data);


            $this->load->view('template/scripts/main',$data);/*scraper and other scripts*/
            $this->load->view('template/public/footer',$data);

        } else {

            /*request login*/
            $data['page_title'] = "PLAY HN";
            $data['menuItems'] = nav_anchor_helper();

            $data['auth_rtnurl'] = current_url();
            $data['auth_conturl'] = current_url(); /*for now just come back to same page where lobby should load - perhaps in future a semi gooey login? e.g where was user going - this could be the continue url in this var right here << */

            $data['message_body'] =   $this->session->flashdata('message');

            $this->load->view('template/public/header',$data);
            $this->load->view('template/public/nav_1',$data);
            $this->load->view('public_pages/play',$data);
            $this->load->view('template/public/footer',$data);
        }
    }

下面是我在登录函数中处理返回URL的方法:登录/身份验证控制器:(使用ion_auth)

function login()
    {
        $this->data['title'] = "Login";

        //validate form input
        $this->form_validation->set_rules('identity', 'Identity', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if ($this->form_validation->run() == true)
        {

            if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
            {
                //if the login is successful
                //redirect them back to the home page
                $this->session->set_flashdata('message', $this->ion_auth->messages());


                $rtnurl = $this->input->post('auth_conturl');
                if(!$rtnurl || $rtnurl == ""){
                    $rtnurl = '/';
                }

                redirect($rtnurl, 'refresh');
            }

这只是登录函数的一个摘录/片段-但正如你所看到的,我利用代码触发器中的函数“redirect”将用户推回到登录表单中发布的返回URL(该URL是在视图/前一个控制器中使用current_url()函数设置的)。
最后我的默认视图文件与登录表单,以显示您我是如何传递返回URL:

<div>

    <h4>Login</h4>
    <div id="infoMessage" class="errortext"><?php echo $message_body;?></div>

    <?php echo form_open('auth/login', array('class' => 'form col')); ?>

      <p>
        <label for="identity">Email:</label>    <input type="text" name="identity" value="" id="identity">  </p>

      <p>
        <label for="password">Password:</label>    <input type="password" name="password" value="" id="password">  </p>

      <p>
        <label for="remember">Remember Me:</label>    <input type="checkbox" name="remember" value="1" id="remember">  </p>

      <p><input type="submit" name="submit" value="Login &raquo;"></p>

    <p>
        <a href="<?php echo site_url(); ?>/auth/forgot_password">Forgot password</a> ?
    </p>


      <input type="hidden" name="auth_rtnurl" value="<?php echo $auth_rtnurl; ?>"/>
      <input type="hidden" name="auth_conturl" value="<?php echo $auth_conturl; ?>"/>

    <?php echo form_close();?>
</div>
6tqwzwtp

6tqwzwtp2#

要将当前动态url用作表单操作,只需使用-

<?= form_open(current_url()); ?>

相关问题