开发者

Stuck in CodeIgniter Controller class implementation

开发者 https://www.devze.com 2023-04-11 05:34 出处:网络
I am recoding my site using CodeIgniter. This is my first time using an MVC or any other type of development pattern. I am using the video tutorials off of the CI site to make a playground and get acq

I am recoding my site using CodeIgniter. This is my first time using an MVC or any other type of development pattern. I am using the video tutorials off of the CI site to make a playground and get acquainted with the protocol of the system.

I've just encountered my first problem and it has to do with a parent class.

I am on the second tutorial trying to make a blog and have this:

<?php

class Test extends CI_Controller {

    function Test()
    {
        parent::CI_Controller();

        $this->load->scaffolding('entries');
    }

    function index() 
    {
        $data['muck'] = 'test test test';
        $data['kookoo'] = 'howdy howdy hi';
        $data['hi'] = 'holla';
        $data['yo'] = 'fa la la';
        $data['zoom'] = '1234';
        $data['array'] = array('hi','howdy','hey','sup');
        $this->load->view('test_view', $data);
    }

}

?>

When I loaded the page without the content inside the function Test() I noticed that the system could not find "Controller". I discovered that the tutorial is using an older version of CI and that "CI_Controller" is the proper name for the class Controller. Now with the above code I'm getting this error:

Fatal error: Call to undefined method CI_Controller::CI_Controller() in /Users/michaelsanger/Sites/CodeIgniter/application/controllers/test.php on line 7

I've scoured and am really not sure why it can't define it.

tha开发者_运维问答nks in advance!


You're mixing up things from different versions, the 1.7 one focusing mainly on PHP4-style class construction (using a method with the same name as the class as constructor, instead that the dedicated magic method __construct() available in php 5)

Also, beware that scaffolding is not present in the latest versions. You didn't say which one are you using, I suppose V2. In case you're using an older version, 1) use the latest :) 2) the parent class was just Controller.

It should be like this

class Test extends CI_Controller {

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

         //$this->load->scaffolding('entries');
     }

     function Test()
     {
      // this will call a method name test, so maps to a URL like Test/test

     }

}

Note that it's not needed to extends the parent controller, unless of course you want to "autoload" a library to have it available to all methods.

CI is known for its great and easy documentation, so whenever you're using tutorial found on the net, expecially if a bit old (in internet terms), make an habit of going to the user_guide (which is also shipped along with the installation files, for local browsing) whenever you have doubts or problems.

For example, check the controllers page, you'll soon see what's wrong with your snippet (and the tutorial, as of today)

UPDATE:

In routes you're setting a route, that maps to a controller(/method). Quoting the changelog:

Version 2.0.0

Release Date: January 28, 2011 Hg Tag: v2.0.0

General changes

PHP 4 support is removed. CodeIgniter now requires PHP 5.1.6.
Scaffolding, having been deprecated for a number of versions, has been removed.

So I don't know what you mean now with scaffolding. Looking at your route, CI expects a controller named "scaffolding_trigger", which has to be rerouted to the controller "scaffolding". If any of those are present, you get the 404 error. Please, choose a version and stick to that, don't mix things! and don't rely on tutorials, they're not always up-to-date with the latest changes.

0

精彩评论

暂无评论...
验证码 换一张
取 消