0
votes

In my project, I am passing parameters through URL like,

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

What I want is to change the URL to

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

Actually I am creating the URLs dynamically like

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

Controller Code :

// 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);
}

View Code:

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 Code :

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]

Is there any method to do this in Codeigniter without using htaccess. I tried adding

//$route['(:any)'] = 'controllername/function/$1';
$routes['channels/playchannel/(:any)'] = 'channels/playchannel/$1';

in application/config/routes.php file. But it returned 404 (Page Not Found) Error.

Can anyone help me to achieve this. Thanks in advance.

4

4 Answers

2
votes

Isn't this already built in?

The segments in the URL, in following with the Model-View-Controller approach, usually represent: example.com/class/function/ID

The first segment represents the controller class that should be invoked. The second segment represents the class function, or method, that should be called. The third, and any additional segments, represent the ID and any variables that will be passed to the controller.

The URI Class and the URL Helper contain functions that make it easy to work with your URI data. In addition, your URLs can be remapped using the URI Routing feature for more flexibility.

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

Your URL should be like: example.com/Controllername/Functionname/ID/OtherID

In your class you get the ID like this:

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

To create such an URL like that you can do multiple things.

$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

New code based on your application:

// 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>
  }
}
}

This should work.

1
votes

In CodeIgniter, you can do this through your routes.php.

In your routes.php,

$routes['controller/method/param1/param2'] = 'controller/method/$1/$2';

for example, you want to have a URL something like this:

http://example.com/video/playchannel/13/free

You can rewrite it via your routes.php like this:

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

And then on your video.php controller,

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
        }
    }
}

So the URL should look like this:

http://example.com/video/playchannel/13/free

Don't use $this->input->get('something') when you are not using a query string on your URL. Hope this helps!

0
votes

In Codeigniter you can get segments using $this->uri->segment(n); // n=1 for controller, n=2 for method, etc

This provides you to retrieve information from your URI strings

consider this example http://example.com/index.php/controller/action/1stsegment/2ndsegment

it will return

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

For the below link you can get the values using input->post

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

example: to get the id & type

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

or for the other link http://site.org/project/controllername/function/1/free

generally for every name from controller seperated with '/' are segments of URL

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

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

for more information visit this link