CodeIgniter Forums
Controller Confused? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Controller Confused? (/thread-14677.html)



Controller Confused? - El Forum - 01-11-2009

[eluser]dezynworks[/eluser]
My laptop is set up w/ Apache, PHP and MySQL so I can develop locally w/o needed a server. I have a site that works fine on my laptop, but crashes once on the server. One controller seems to be confused. When I go to /gallery, it doesn't call the index() function it calls the view() function. Again, this behavior is only on the server. It works fine on my laptop. Other controllers are working fine in both places. Any ideas what is going on?

-Sean


Controller Confused? - El Forum - 01-11-2009

[eluser]Adam Griffiths[/eluser]
Is the server setup in the same way as your local development environment? Do they both have the same modules installed etc? Is mod_rewrite.c installed?


Controller Confused? - El Forum - 01-12-2009

[eluser]obiron2[/eluser]
if your live server is Linux or Unix based then files will be case sensitive.

check that you have followed strict naming conventions.


Controller Confused? - El Forum - 01-12-2009

[eluser]dezynworks[/eluser]
The server seems to be set up correctly. If there were a server configuration problem, I would expect the whole site to be down, not just the galleries. There are two other controllers used on the site and they work fine. The only real difference is neither one loads a model. The Gallery controller is being called, it's just not calling the right action function. Going to /gallery/ should be calling the index() function in the Gallery controller, but for some reason I cannot fathom, the view() function is being called instead.

I did double check case and the only problem I noticed is, according to the User Guide, a model should be loaded by its class name, not its file name. IOW $this->load->model('Gallery_model'), not $this->load->model('gallery_model'). I did fix this, but it did not make a difference.

I'm getting two errors when I try to access the gallery index page. The first a database error because it is calling the view function and not the index function so the parameters the query is looking for are not there.

The second error is a "Call to a member function on a non-object" error. This one I don't understand, because this line is calling a function in a model (marked w/ ** below). I would expect more parameter based errors not the inability to even access the model. Note: the Gallery model loaded in the constructor is accessible, just not the Photo model loaded in the view function. I don't know if this is another symptom of whatever is causing the wrong action function to be called or not.

This is my Gallery controller:

Code:
class Gallery extends Controller
{
    function Gallery()
    {
        parent::Controller();
        $this->load->model("Gallery_model");
    }
    
    function index()
    {
        $dud = new stdClass();
        $dud->id = 0;
        $data["gallery"] = $dud;
        $data["gallery_menu"] = $this->Gallery_model->fetchAllGalleries();
        $data["galleries"] = $this->Gallery_model->fetchCurrentGalleries();
        $data['content_for_layout'] = $this->load->view( "galleries/index",$data, true );
        $data['css_for_layout'] = 'galleries';
        $data['title_for_layout'] = 'QUAC Photo Galleries';
        $this->load->view( "default_layout", $data );
    }

    function view( $gallery_id )
    {
        $this->load->model( "Photo_model" );

        $gallery = $this->Gallery_model->findById($gallery_id);
        $data["gallery"] = $gallery;
        $data["gallery_menu"] = $this->Gallery_model->fetchAllGalleries();

        **$data["photos"] = $this->Photo_model->findByGalleryId( $gallery_id );
        $data["content_for_layout"] = $this->load->view( "galleries/view", $data, true );
        $data['scripts_for_layout'] = 'jquery.lightbox';
        $data['css_for_layout'] = array('galleries','jquery.lightbox');
        $data['title_for_layout'] = 'QUAC Photo Galleries: '.$gallery->name;
        $this->load->view( "default_layout", $data );
    }
}

And my Photo model:

Code:
class Photo_model extends Model
{
    function Photo_model(){
        parent::Model();
    }

    function findByGalleryId( $gid ){
        $this->db->where( "gallery_id", $gid );
        $cursor = $this->db->get("photos");
        return $cursor->result();
    }
}

I've looked through my config files, and I don't see anything obviously wrong. Neither of these models is autoloaded.

Thanks for your feedback. Any other ideas for what I need to check?

-Sean


Controller Confused? - El Forum - 01-12-2009

[eluser]Colin Williams[/eluser]
view() is a reserved method name if your server is PHP 4. You might be on PHP 5 locally and PHP 4 on production


Controller Confused? - El Forum - 01-13-2009

[eluser]dezynworks[/eluser]
That was it! Thank you!