CodeIgniter Forums
Controller not working as expected - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Controller not working as expected (/showthread.php?tid=43739)



Controller not working as expected - El Forum - 07-21-2011

[eluser]StefanAlexandru[/eluser]
Hi everyone, I am new to CI, and I am trying to build up a classified site , so I've just encountered a problem which I couldn't understand.

So here is my code :

Controller :
Code:
<?php
class Ads extends CI_Controller {


        public function index(){
                $this->load->model('General');
                 $data['anunt']=$this->General->adbyID();
                 $this->load->view('anunt', $data);
        }

      


                
      
}
?>

Model:
Code:
class General extends CI_Model {

        function __construct() {
                parent::__construct();
    }

        public function adbyID() {
                $id=$this->uri->segment(2);
                $this->db->where('postID', $id);
                $query=$this->db->get('v_ads');

                return $query->result();
         }
}


So, when I am calling the controller like this :
http://www.domain.net/subdomain/ads/10/
I am getting a 404 error, but if I make a new function, not putting all the code in the index function, everything is working fine.

So my question is : Why I can't perform that actions in the index function of the controller ?

It wouldn't be a real problem but it's for seo purposes to have
Code:
http://www.domain.net/subdomain/ad/10/
instead of
Code:
http://www.domain.net/subdomain/ads/function/10/

Thanks in advance and sorry for my bad english.


Controller not working as expected - El Forum - 07-21-2011

[eluser]danmontgomery[/eluser]
Default controller is only called when one isn't specified. In your example, you're specifying the "10" method, which doesn't exist. You can get around this with routing, which is detailed in the user guide.


Controller not working as expected - El Forum - 07-21-2011

[eluser]StefanAlexandru[/eluser]
Thanks for your fast reply, so this is how i've made it, maybe others will encounter this problem too:

routes.php
Code:
$route['ad/:num']= '/ad/adbyid';

controller:
Code:
<?php
class Ad extends CI_Controller {


        public function index(){
              
        }

       public function adbyid(){
          $this->load->model('General');
           $data['anunt']=$this->General->adbyID();
           $this->load->view('anunt', $data);
}
                
      
}
?>



Controller not working as expected - El Forum - 07-21-2011

[eluser]Twisted1919[/eluser]
forget it Smile


Controller not working as expected - El Forum - 07-21-2011

[eluser]StefanAlexandru[/eluser]
What do you exactly mean by naming convention on models ?
instead of General should i use Generalmodel or General_model ?

LaterEdit:

Ok :-)