在我的项目中,我通过URL传递参数,如
http://site.org/project/controllername/function?id=1&type=free
我想要的是将网址更改为
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>
}
}
}
Htaccess代码:
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]
有没有任何方法可以在不使用htaccess的情况下在Codeigniter中执行此操作。我尝试添加
//$route['(:any)'] = 'controllername/function/$1';
$routes['channels/playchannel/(:any)'] = 'channels/playchannel/$1';
application / config / routes.php 文件中的。但它返回了404 (Page Not Found) Error
。
任何人都可以帮助我实现这一目标。提前谢谢。
答案 0 :(得分:2)
这不是已内置的吗?
以下模型 - 视图 - 控制器方法中的URL中的段通常表示: example.com/class/function/ID
第一个段表示应该调用的控制器类。 第二个段表示应该调用的类函数或方法。 第三个和任何其他段表示ID和将传递给控制器的任何变量。
URI类和URL帮助程序包含的函数使您可以轻松处理URI数据。此外,您可以使用URI路由功能重新映射您的URL,以获得更大的灵活性。
来源:http://ellislab.com/codeigniter/user-guide/general/urls.html
您的网址应该是:example.com/Controllername/Functionname/ID/OtherID
在课堂上你会得到这样的ID:
class Controllername
{
function Functionname($ID = FALSE, $OtherID = FALSE)
{
$ID_from_url = $ID;
$OtherID_from_url = $OtherID;
}
}
要创建这样的网址,您可以执行多项操作。
$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>
}
}
}
这应该有用。
答案 1 :(得分:0)
在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
答案 2 :(得分:0)
对于以下链接,您可以使用input-&gt; post
获取值http://site.org/project/controllername?id=1&type=free
示例:获取id&amp;型
//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">
了解更多信息visit this link
答案 3 :(得分:0)
在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
当您未在网址上使用查询字符串时,请勿使用$this->input->get('something')
。
希望这有帮助!