Codeigniter中以斜杠分隔的URL参数


URL parameter in Codeigniter separated by slashes

在我的项目中,我通过URL传递参数,如

http://site.org/project/controllername/function?id=1& type =免费

我想把URL改为

http://site.org/project/controllername/function/1/free

实际上我是动态创建url,比如

<a href="<?php echo base_url()?>controllername/function?id=<?php echo $defaultrow->id;?>&type=free">

控制器代码:

// Inside VideoController
function search()
{
        $userid=$this->tank_auth->get_user_id();    
        $data['userid']=$userid;
        $time=time();
        $data['defaultvideoquery']=$this->db->query("select cv.videotitle,cv.video,cv.thumbnail,
                cv.added_on,cp.description,cv.id,cv.likes,cv.dislikes,cv.views from channel_programmes as cp INNER JOIN channel_videos as cv
                 ON cp.programme_source=cv.id and cp.channel_id=0 and $time>=cp.starttime and $time>=cp.endtime and cp.status=1");
        $data['videonum']=$data['defaultvideoquery']->num_rows();
        $data['page']='video/search';
        $this->load->view('layout/template',$data);
}
// Inside ChannelsController
function playchannel()
{
    if (!$this->tank_auth->is_logged_in()) 
    {       // not logged in
        redirect('auth/login');
    }
    $data['id']=$userid;
    $id=$this->input->get('id');
    if($this->input->get('type'))
    {
        if($this->input->get('type')=="free")
        {
            $time=time();
            $data['currentdate']=$time;
            $pgmquery=$this->db->query("select cp.channel_id,cp.title,cp.programme_source,cp.url,cp.description,cv.status,cv.video,cp.starttime,cp.endtime from
            channel_videos as cv INNER JOIN channel_programmes as cp ON cv.id=cp.programme_source where cp.status=1 and cv.id=$id");
        }
        else
        {
            $query=$this->db->query("select id,title,thumbnail,channel_type,created_on,status,channel_type,description from videochannel
             where created_for=$userid and id=$id");
            $data['queryresult']=$query->row();
            $data['channeldata']=$query->result();
        }
        $data['pgmnum']=$pgmquery->num_rows();
        $data['pgmresult']=$pgmquery->result();
    }
    $data['page']='channels/playchannel';
    $this->load->view('layout/template',$data);
}

视图代码:

if(isset($videonum))
{
    if($videonum>0)
    {
         foreach($defaultvideoquery->result() as $defaultrow)
         {?>
            <a href="<?php echo base_url()?>channels/playchannel?id=<?php echo $defaultrow->id;?>&type=free">
        <?php if($defaultrow->thumbnail=="")
        { ?>
            <img src="<?php echo base_url();?>images/No_image.png" class="videothumb" alt="Video thumbnail <?php echo $defaultrow->videotitle;?>">
      <?php } 
        else
        { ?>
            <img src="<?php echo base_url();?>channel/<?php echo $defaultrow->id;?>/<?php echo $defaultrow->thumbnail;?>" class="videothumb" alt="Video thumbnail <?php echo $defaultrow->videotitle;?>">   
    <?php   } ?>
        </a>
      }
    }
}

访问代码:

RewriteEngine On
RewriteBase /projectname/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule '.htaccess - [F]
RewriteRule (.*)(css'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(css'/smoothness'/images'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(js'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(js'/user'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(js'/user'/timeslots'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(channel_data'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(images'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(sliders'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(secure'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(userdata'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(channel'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(css'/admin'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(css'/screen'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(css'/skins'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(css'/fonts'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(css'/images'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(css'/smoothness'/)(.*)$ $2$3 [QSA,L]
RewriteRule (.*)(fonts'/)(.*)$ $2$3 [QSA,L]
RewriteCond %{request_uri} !^index'.php
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

在Codeigniter中是否有任何方法可以在不使用htaccess的情况下做到这一点?我试着添加

//$route['(:any)'] = 'controllername/function/$1';
$routes['channels/playchannel/(:any)'] = 'channels/playchannel/$1';
application/config/routes.php文件中的

。但是它返回404 (Page Not Found) Error

有谁能帮我实现这个吗?

这不是已经内置的吗?

URL中的片段,在遵循模型-视图-控制器方法时,通常表示:example.com/class/function/ID

第一个段表示应该调用的控制器类。第二段表示应该调用的类函数或方法。第三段,以及任何其他段,表示ID和将传递给控制器的任何变量。

URI类和URL Helper包含的函数使处理URI数据变得容易。此外,您的url可以使用URI Routing特性重新映射,以获得更大的灵活性。

来源:http://ellislab.com/codeigniter/user-guide/general/urls.html

你的URL应该像:example.com/Controllername/Functionname/ID/OtherID

在你的类中你得到这样的ID:

class Controllername
{
   function Functionname($ID = FALSE, $OtherID = FALSE)
   {
       $ID_from_url = $ID;
       $OtherID_from_url = $OtherID;
   }
}

要创建这样一个URL,你可以做很多事情。

$this->load->helper('url');
echo site_url("news/local/123");  // http://example.com/index.php/news/local/123
echo base_url("blog/post/123");  // http://example.com/blog/post/123

基于应用程序的新代码:

// function
function playchannel($id = false, $condition = false)
{
if (!$this->tank_auth->is_logged_in()) 
{       // not logged in
    redirect('auth/login');
}
$data['id']=$userid;
if($condition)
{
    if($condition=="free")
    {
        $time=time();
        $data['currentdate']=$time;
        $pgmquery=$this->db->query("select cp.channel_id,cp.title,cp.programme_source,cp.url,cp.description,cv.status,cv.video,cp.starttime,cp.endtime from
        channel_videos as cv INNER JOIN channel_programmes as cp ON cv.id=cp.programme_source where cp.status=1 and cv.id=$id");
    }
    else
    {
        $query=$this->db->query("select id,title,thumbnail,channel_type,created_on,status,channel_type,description from videochannel
         where created_for=$userid and id=$id");
        $data['queryresult']=$query->row();
        $data['channeldata']=$query->result();
    }
    $data['pgmnum']=$pgmquery->num_rows();
    $data['pgmresult']=$pgmquery->result();
}
$data['page']='channels/playchannel';
$this->load->view('layout/template',$data);
}
//view 
if(isset($videonum))
{
if($videonum>0)
{
     foreach($defaultvideoquery->result() as $defaultrow)
     {?>
        <a href="<?php echo base_url()?>channels/playchannel/<?php echo $defaultrow->id;?>/free">
    <?php if($defaultrow->thumbnail=="")
    { ?>
        <img src="<?php echo base_url();?>images/No_image.png" class="videothumb" alt="Video thumbnail <?php echo $defaultrow->videotitle;?>">
  <?php } 
    else
    { ?>
        <img src="<?php echo base_url();?>channel/<?php echo $defaultrow->id;?>/<?php echo $defaultrow->thumbnail;?>" class="videothumb" alt="Video thumbnail <?php echo $defaultrow->videotitle;?>">   
<?php   } ?>
    </a>
  }
}
}

在CodeIgniter中,你可以通过你的routes.php来实现。

在你的routes.php中,
$routes['controller/method/param1/param2'] = 'controller/method/$1/$2';
例如,你想要一个像这样的URL:
http://example.com/video/playchannel/13/free

你可以通过你的routes.php重写它,像这样:

$routes['video/playchannel/(:any)/(:any)'] = 'video/playchannel/$1/$2';

然后在video。php控制器上,

Class Video {
    function __construct() {
    }
    public function playchannel( $id, $type ) {
        // do process
        if (!$this->tank_auth->is_logged_in()) {  
          // not logged in
          redirect('auth/login');
        }
        $data['id'] = $userid;
        if( $type == 'free' ) {
           // do something here
        }
    }
}
所以URL应该是这样的:
http://example.com/video/playchannel/13/free

在URL上不使用查询字符串时不要使用$this->input->get('something')。希望这对你有帮助!

在Codeigniter中,您可以使用$this->uri->segment(n);//n=1为控制器,n=2为方法等获得段

这使您可以从URI字符串

中检索信息

考虑这个例子http://example.com/index.php/controller/action/1stsegment/2ndsegment

它将返回

$this->uri->segment(1); // controller
$this->uri->segment(2); // action
$this->uri->segment(3); // 1stsegment
$this->uri->segment(4); // 2ndsegment

对于下面的链接,您可以使用input->post

获取值

http://site.org/project/controllername?id=1& type =免费

示例:获取id &类型

//add this in controller function.
$this->input->post('id');

或其他链接http://site.org/project/controllername/function/1/free

通常对于来自控制器的每个名称,用'/'分隔是URL的段

$this->uri->segment(n);
<a href="<?php echo site_url();?>controllername/function?id=<?php echo $defaultrow->id;?>&type=free">

更多信息请访问此链接