【问题标题】:Pass url from current page to controller in Codeigniter将当前页面的 url 传递给 Codeigniter 中的控制器
【发布时间】:2021-06-14 11:04:23
【问题描述】:

我想将当前页面 url 从 views (user/index.php) 传递给 controllers (User.php),因为我想使用 url 参数在控制器中。

顺便说一下,我的系统只有一个编辑页面(user/index.php),这个页面将在两个不同的页面中使用,(views/home.php) (views/faculty/detail.php)。这两个页面中的每个 编辑按钮 我都创建了一个 href 来指示这两个页面中的任何一个来编辑页面。在 href 的末尾,我把参数 eg: edit button for detail.php href="<?php echo base_url()?>index.php/user/index/<?php echo $user->user_no ?>?prevpage=index.php/faculty/detail/"user/index.php 将在单击编辑按钮后显示一个 url http://[::1]/Test-bank/index.php/user/index/76?prevpage=index.php/faculty/detail/detail.php 的编辑按钮 href="<?php echo base_url()?>index.php/user/index/<?php echo $user->user_no ?>?prevpage=index.php/home/"user/index.php 将显示点击编辑按钮 http://[::1]/Test-bank/index.php/user/index/1?prevpage=index.php/home/ 的 url。

我想将这些 URL 参数 ?prevpage=index.php/home/?prevpage=index.php/faculty/detail/ 传递给控制器​​ (User.php) 并将其设为字符串以便我将其用于 redirect(base.url());,以便我可以重定向编辑页面 (user/index.php) 返回到它上一页 (views/home.php) 或 (views/faculty/detail.php)

(views/home.php)

    <div>
         <a href="<?php echo base_url()?>index.php/user/index/<?php echo $user->user_no ? >
          ?prevpage=index.php/home/" class="btn btn-primary">Edit Info</a>
    </div>

(views/faculty/detail.php)

    <div>
         <a href="<?php echo base_url()?>index.php/user/index/<?php echo $user->user_no ?>
          ?prevpage=index.php/faculty/detail/" class="btn btn-primary">Edit Info</a>
    </div>

控制器(User.php)

    <?php
       session_start();
       defined('BASEPATH') OR exit('No direct script access allowed');

       class User extends CI_Controller {
       function __construct()
     {
       // this is your constructor
        parent::__construct();
        $this->load->model('User_Model');
        $this->load->database();
        $this->load->helper('form');
        $this->load->helper('url');
     }

      public function index($user_no = ''){

     if(!isset($_SESSION['authorization'])  || ($_SESSION['authorization'] != '1' && $user_no != 
      $_SESSION['user_no'])){
        redirect(base_url().'index.php','refresh');
        exit();
    }
    $user = $this->User_Model->get_user($user_no);
    $data = array(
        'user'      =>  $user,
    );
    $this->load->view('user/index',$data);
    }

    public function save(){
    $user_no = $this->input->post('user_no') + 0;
    $is_user_full       = $this->User_Model->is_user_full();
    if($is_user_full){
        $_SESSION['result'] = 'ERROR: User List Full';
        redirect(base_url().'index.php/user/index/'.$user_no);
    }
    $is_username_exists = $this->User_Model->check_username($this->input->post('username'));
    if($is_username_exists && $user_no == 0){
        $_SESSION['result'] = 'ERROR: Username Exists';
        redirect(base_url().'index.php/user/index/'.$user_no);
    }
    $data = array(
        'first_name'        =>  $this->input->post('first_name'),
        'last_name'         =>  $this->input->post('last_name'),
        'username'          =>  $this->input->post('username'),
        'authorization'     =>  $this->input->post('authorization') + 0,
    );
    if( !empty($this->input->post('password')) && !empty($this->input->post('cpassword')) ){
        $data['password']   =  $this->input->post('password');
    }
    if($user_no > 0){
        $result = $this->User_Model->update($data,$user_no);
        $redirect = base_url().'******here where I will put the parameter***';

    }
    else{
        $result = $this->User_Model->create($data,$user_no);
        $redirect = base_url().'index.php/faculty/';

    }
    $_SESSION['result'] = ($result) ? 'SUCCESS!' : 'ERROR!';
    redirect($redirect);

    }

我不知道在那里做什么。希望你能帮助我。

【问题讨论】:

  • 如果您在谈论prevpage,请使用$this-&gt;input-&gt;get('prevpage')

标签: php html codeigniter


【解决方案1】:

在您的视图上进行隐藏输入:

<?php
    $actual_link                = "0$_SERVER[REQUEST_URI]";
?>

<input type="hidden" id="yourID" name="yourName" autocomplete='off' class="form-control" value="<?php echo $actual_link?>">

在控制器上添加:

$data = array(
    ...
    'thelink'    =>    $this->input->post('yourName'),
    ...
);

【讨论】:

    【解决方案2】:

    有两种方法可以做到这一点。 要么使用get函数来获取你这样设置的参数。

    $redirect = base_url().$this->input->get('prevpage');
    

    或者您可以像这样使用 $_SERVER['HTTP_REFERER'] 来获取引荐来源网址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-04
      • 2011-11-03
      • 2011-02-14
      • 2016-08-29
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多