【问题标题】:Codeigniter - url segment replace or redirectCodeigniter - url 段替换或重定向
【发布时间】:2013-03-29 02:45:53
【问题描述】:

我有一个基本控制器 (base),所有其他控制器都从该控制器扩展。 放在这里的任何东西都会覆盖其他控制器,重定向会在这里。

网址示例:

 http://domain.com/controllerone/function
 http://domain.com/controllertwo/function
 http://domain.com/controllerthree/function

使用下面的代码。会给我控制器名称

$this->uri->segment(1);

上述每个控制器都需要重定向到单独的 URL,但功能部分不应该改变:

 http://domain.com/newcontrollerone/function
 http://domain.com/newcontrollertwo/function
 http://domain.com/newcontrollerthree/function

在我的基本控制器中,我想要以下逻辑:

    $controller_name =   $this->uri->segment(1);

    if($controller_name === 'controllerone'){
       // replace the controller name with new one and redirect, how ?
    }else if($controller_name === 'controllertwo'){
     // replace the controller name with new one and redirect, how ?
    }else{
        // continue as normal
   }  

我在想我应该使用redirect() 函数和str_replace(),但不知道它们的效率如何。理想情况下,我不想使用Routing 类。

谢谢。

【问题讨论】:

    标签: codeigniter url redirect


    【解决方案1】:

    使用 segment_array 的简单解决方案:

    $segs = $this->uri->segment_array();
    
    if($segs[1] === 'controllerone'){
      $segs[1] = "newcontroller";
       redirect($segs);
    }else if($segs[1] === 'controllertwo'){
       $segs[1] = "newcontroller2";
       redirect($segs);
    }else{
        // continue as normal
    } 
    

    【讨论】:

      【解决方案2】:

      CodeIgniter's URI Routing,在这种情况下应该可以提供帮助。但是,如果您有充分的理由不使用它,那么此解决方案可能会有所帮助。

      潜在的重定向在一个数组中,其中 key 是在 URL 中查找的控制器名称,value 是要重定向到的控制器的名称.这可能不是最有效的,但我认为它应该比可能很长的if-then-else 语句更易于管理和阅读。

      //Get the controller name from the URL
      $controller_name = $this->uri->segment(1);
      //Alternative: $controller_name = $this->router->fetch_class();
      
      //List of redirects
      $redirects = array(
          "controllerone" => "newcontrollerone",
          "controllertwo" => "newcontrollertwo",
          //...add more redirects here
      );
      
      //If a redirect exists for the controller    
      if (array_key_exists($controller_name, $redirects))
      {
          //Controller to redirect to
          $redirect_controller = $redirects[$controller_name];
          //Create string to pass to redirect
          $redirect_segments = '/'
                             . $redirect_controller
                             . substr($this->uri->uri_string(), strlen($controller_name)); //Function, parameters etc. to append (removes the original controller name)
          redirect($redirect_segments, 'refresh');    
      }
      else
      {
          //Do what you want...
      }
      

      【讨论】:

      • 谢谢回复,应该是redirect($redirect_url, 'refresh'); >> redirect($redirect_segments, 'refresh'); ?
      【解决方案3】:

      试试

      header("Location:".base_url("newcontroller/".$this->uri->segment(2)));
      

      【讨论】:

      • 我认为 redirect('newcontroller/'.$this->uri->segment(2)); 会更符合 CodeIgniter 的习惯。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 2015-11-12
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多