ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Codeigniter METHOD ??? -error ?

October 07, 2010 6:18am

Subscribe [2]
  • #1 / Oct 07, 2010 6:18am

    sarah fox

    9 posts

    Hi,

    Looking at :- http://ellislab.com/codeigniter/user-guide/ - I see controllers, models, views. helpers etc..

    But nothing about METHODS .

    I’ve done a debug of my scripts & THINK I found a problem as to why CI doesn’t work on my WIN XP box.

    C:\usr\local\lib\codeigniter\current\system\codeigniter\CodeIgniter.php

    echo "if ( ! class_exists($class)
    
        OR $method == 'controller'
    
        OR strncmp($method, '_', 1) == 0
    
        OR in_array(strtolower($method), array_map('strtolower', get_class_methods('Controller')))
    
    \n";
    
    echo "<b>
    if ( ! class_exists(".$class.")
    
        OR ".$method." == 'controller'
    
        OR strncmp(".$method.", '_', 1) == 0
    
        OR in_array(strtolower(".$method."), array_map('strtolower', get_class_methods('Controller')))</b>
    
    \n";
    
    
    
    if ( ! class_exists($class)
        OR $method == 'controller'
        OR strncmp($method, '_', 1) == 0
        OR in_array(strtolower($method), array_map('strtolower', get_class_methods('Controller')))
        )
    {
        show_404("{$class}/{$method}");
    }


    Here is my output :-

    if ( ! class_exists(home)
    OR index == ‘controller’
    OR strncmp(index, ‘_’, 1) == 0
    OR in_array(strtolower(index), array_map(‘strtolower’, get_class_methods(‘Controller’)))

    if ( ! class_exists(home)
    OR index == ‘controller’
    OR strncmp(index, ‘_’, 1) == 0
    OR in_array(strtolower(index), array_map(‘strtolower’, get_class_methods(‘Controller’)))</code></pre>


    It seems as if STRNCMP is the cause - as my ‘method’ doesn’t contain an underline character - It displays the 404 screen.


    is my logic correct ?

    How do I find / Fix my method ?

    Ps, my root / main controller is /controllers/home.php (is correct)

  • #2 / Oct 07, 2010 6:55am

    sarah fox

    9 posts

    its not my Method

    It seems to e an error with my in_array command.

    Code :-

    $a= strncmp(".$method.", '_', 1);
    echo "STRCOMPARE1 - $a
    \n";
    $a=class_exists($class);
    echo "STRCOMPARE2 - $a
    \n";
    $a=(in_array(strtolower($method), array_map('strtolower', get_class_methods('Controller'))));
    echo "STRCOMPARE3 - $a
    \n";

    Output :-

    STRCOMPARE1 - -1
    STRCOMPARE2 - 1
    STRCOMPARE3 -


    So, as in_array isn’t producing any results - it activates the 404 page ?


    is this right / Correct ?

    Can someone advise how to fix it, & what that line actually means (in emglish)...


    Ps, using my friends login, incorrectly….

  • #3 / Oct 07, 2010 7:29am

    WanWizard

    4475 posts

    in_array returns a boolean, which doesn’t display with echo.

    try

    echo "STRCOMPARE3 - ",($a ? "TRUE" : "FALSE"),"
    \n";
  • #4 / Oct 07, 2010 9:23am

    sarah fox

    9 posts

    $a= strncmp(”.$method.”, ‘_’, 1);<br /> echo “STRCOMPARE1 - $a<br /> \n”;<br /> $a=class_exists($class);<br /> echo “STRCOMPARE2 - $a<br /> \n”;<br /> $a=(in_array(strtolower($method), array_map(‘strtolower’, get_class_methods(‘Controller’))));<br /> echo “STRCOMPARE3 - $a<br /> \n”;</p> <p>echo “STRCOMPARE32 - “,($a ? “TRUE” : “FALSE”),”<br /> \n”;


    STRCOMPARE32 = FALSE

    EDIT :- so, something in the ‘in_array’ command - Produces ‘false’ - which results in the 404 error.

    Are there any steps I can take - How do I check what should be / Is in this array… - it seems to combine 3 functions (None of which I am familiar with)...

  • #5 / Oct 07, 2010 9:31am

    WanWizard

    4475 posts

    Show us that home controller. It’s very unlikely that this is a CI issue…

  • #6 / Oct 07, 2010 9:38am

    sarah fox

    9 posts

    Ive removed comment lines starting with //

    <?php

    class Home extends Controller {

    function Home()
    {
    parent::Controller();
    echo "VIEWING ROOT FILE
    \n";

    }

      function about()
      {

    $data['head_text'] = trim("Title of page");

    $this->smarty_parser->parse('about.tpl',$data);
    }

      function safety()
      {

    $data['head_text'] = trim("Title of page");

    $this->smarty_parser->parse('safety.tpl',$data);
    }


      function examples()
      {

    /// LOGS DEBUG
    //$this->output->enable_profiler(TRUE);
    $data['head_text'] = trim("Title of page");

    $this->smarty_parser->parse('examples.tpl',$data);
    }

      function goto()
      {
    if ($where = $this->input->post('newpage'))
    redirect($where);
    else
    echo "GO WHERE $where
    \n";
    }


      function index()
      {

    /// LOGS DEBUG
    $this->output->enable_profiler(TRUE);
    // error_reporting(E_ALL);
    // $data["rotate_head_message"]=header_message();
    $data['show_map'] = "no";
    $data['head_text'] = trim("Title of page");
    #print_r($data);
    # $pieces_title = explode(" ", $data['head_title');

    // Title random number
    $r=rand(1,100);
    $data['title_random'] = $r;

    $this->load->model('Header_rotate');
    // If using multiple templates to display a page
    // then each except the last one should be "displayed"
    // to send it to the screen.
    $this->smarty_parser->parse("mainpage.tpl", $data);
    }


    }
    /* End of file root.php */
    /* Location: ./system/application/controllers/welcome.php */

     


    (Ive renamed the page from welcome/root/home etc - But all file-names are correct.. )

    EDIT:-  /controllers/home.php (correct / new filename)

  • #7 / Oct 07, 2010 11:02am

    WanWizard

    4475 posts

    Do you have error reporting enabled, and display errors set to ‘on’ in php.ini?

    If so, you would have seen errors in your log files.

    The issue here is that the class doesn’t load, because ‘goto’ is a reserved word, that causes a parse error.

  • #8 / Oct 07, 2010 12:45pm

    techgnome

    182 posts

    I’m still fairly new to CI, but not programming… where is your $this->smarty_parser loaded? And I’m not sure what you mean about there being nothing about methods… the underscore is for “private” methods… meaning that they can’t be accessed via the URL. Example: currently, with the controller above, the following url will work: index.php/home/about .... but if you were to change it to function _about .... then index.php/home/_about would not work. How ever, from within the controller $this->_about would work.

    Good call on that Wan - I didn’t even see that.

    -tg

  • #9 / Oct 08, 2010 1:56am

    sarah fox

    9 posts

    Do you have error reporting enabled, and display errors set to ‘on’ in php.ini?

    If so, you would have seen errors in your log files.

    what errors in logs ?

    php.ini

    error_reporting = E_ALL & E_NOTICE
    display_errors = On
    error_log = ‘c:\phpnew\logs\error.log’

    (I also had error_log = /phpnew/logs/error.log )

    - No logs & no php errors - Directory does exist…

    PS - its the “Codeigniter” 404 file being displayed - Not a incorrect web-page (browser 404 page)... - so PHP is being run - & if I put phpinfo(); I do get results…

    The issue here is that the class doesn’t load, because ‘goto’ is a reserved word, that causes a parse error.

    I’ll remember that - for future, but thats still not the issue…

    I saved my home.php script into a new name, & adjusted the controllers/home.php script as so :- (taking out a lot)..

    Took out a LOT of functions - Bare basics…

    <?php

    class Home extends Controller {

    function Home()
    {
    parent::Controller();
    echo "VIEWING ROOT FILE
    \n";

    }


      function index()
      {

    /// LOGS DEBUG
    $this->output->enable_profiler(TRUE);
    $data['show_map'] = "no";
    $data['head_text'] = trim("Title of page");

    $r=rand(1,100);
    $data['title_random'] = $r;

    $this->load->model('Header_rotate');
    $this->smarty_parser->parse("mainpage.tpl", $data);
    }


    }
    /* End of file root.php */
    /* Location: ./system/application/controllers/welcome.php */

     

    Still no go…

    I’ll check the $this->smarty_parser thing - However - Even if the templates don’t get parsed as “smarty” - They’ll be loaded / parsed as “something” (even if incorrect HTML code is shown…)

  • #10 / Oct 08, 2010 5:37am

    WanWizard

    4475 posts

    I’ve copied your home controller into a fresh CI 1.7.2 install here, and get the parse error.
    You get the CI 404 error because ‘get_class_methods(‘Controller’)’ doesn’t return data, since the class didn’t load.

    If I change ‘goto’ to ‘x’, the home controller loads without problems.

    If I copy the stripped home controller code in as per your post, it loads without problems (I get the “VIEWING ROOT FILE” text, and the can’t find the model error).

    Just checking, this controller is in /system/application/controllers/home.php ?

  • #11 / Oct 08, 2010 6:44am

    sarah fox

    9 posts

    C:\home\sites\SITE-NAME\application\controllers\home.php = Yes

    Tomorrow, I may just remove / rename the directories - & re-install Apache / CI from scratch

    & may install my own “debug” class (If i’m bothered..) ) - To tell us (me) more information.. - But thats long-term not short-term 😊

  • #12 / Oct 08, 2010 7:35am

    sarah fox

    9 posts

    Ok.. - COOL tool.

    I used the PHP ‘backtrace’ command - (didn’t know of it before) - Though I’ve figured it goes from bottom-up..

    Here is the backtrace

    I guess I’ll re-install Smarty…

    #0 include() called at [C:\usr\local\lib\codeigniter\current\system\libraries\Exceptions.php:127]
    #1 CI_Exceptions->show_error(An Error Was Encountered, Unable to load the requested class: Smarty_parser, error_general, 500) called at [C:\usr\local\lib\codeigniter\current\system\codeigniter\Common.php:238]
    #2 show_error(Unable to load the requested class: Smarty_parser) called at [C:\usr\local\lib\codeigniter\current\system\libraries\Loader.php:764]
    #3 CI_Loader->_ci_load_class(smarty_parser, , ) called at [C:\usr\local\lib\codeigniter\current\system\libraries\Loader.php:96]
    #4 CI_Loader->library(smarty_parser) called at [C:\usr\local\lib\codeigniter\current\system\libraries\Loader.php:999]
    #5 CI_Loader->_ci_autoloader() called at [C:\usr\local\lib\codeigniter\current\system\libraries\Controller.php:83]
    #6 Controller->_ci_initialize() called at [C:\usr\local\lib\codeigniter\current\system\libraries\Controller.php:43]
    #7 Controller->Controller() called at [C:\home\sites\WEBSITE\application\controllers\home.php:11]
    #8 Home->Home() called at [C:\usr\local\lib\codeigniter\current\system\codeigniter\CodeIgniter.php:201]
    #9 require_once(C:\usr\local\lib\codeigniter\current\system\codeigniter\CodeIgniter.php) called at [C:\home\sites\WEBSITE\index.php:143]

  • #13 / Oct 09, 2010 3:11am

    sarah fox

    9 posts

    IDENTIFIED ERROR FILE :- C:\usr\local\lib\codeigniter\current\system\libraries\Loader.php

    I’m getting close, & I’ve also identified a flaw in the order / process of the Loader.php file..

    First of, My output (I added a LOT of log_message lines 😊 )

    DEBUG - 2010-10-09 18:58:40—> Config Class Initialized
    DEBUG - 2010-10-09 18:58:40—> Hooks Class Initialized
    DEBUG - 2010-10-09 18:58:40—> URI Class Initialized
    DEBUG - 2010-10-09 18:58:40—> No URI present. Default controller set.
    DEBUG - 2010-10-09 18:58:40—> Router Class Initialized
    DEBUG - 2010-10-09 18:58:40—> Output Class Initialized
    DEBUG - 2010-10-09 18:58:40—> Input Class Initialized
    DEBUG - 2010-10-09 18:58:40—> Global POST and COOKIE data sanitized
    DEBUG - 2010-10-09 18:58:40—> Language Class Initialized
    DEBUG - 2010-10-09 18:58:40—> Loader Class Initialized - View path /home/sites/SITENAME/application/views/
    DEBUG - 2010-10-09 18:58:40—> Helper loaded: url_helper
    DEBUG - 2010-10-09 18:58:40—> Helper loaded: my_headermessage_helper
    DEBUG - 2010-10-09 19:03:52—> loader.php - Library function - Library ‘smarty_parser’
    DEBUG - 2010-10-09 19:03:52—> loader.php - Library function - Not array - library - ‘smarty_parser’
    DEBUG - 2010-10-09 19:03:52—> loader.php - _ci_load_class function - class=smarty_parser
    DEBUG - 2010-10-09 19:03:52—> loader.php - _ci_load_class function - class 2 ‘smarty_parser’
    DEBUG - 2010-10-09 19:03:52—> loader.php - _ci_load_class function - Upper/Lower array ‘Smarty_parser’
    DEBUG - 2010-10-09 19:03:52—> loader.php - _ci_load_class function - SUBCLASS NOW ‘/home/sites/SITENAME/application/libraries/MY_Smarty_parser.php’
    DEBUG - 2010-10-09 19:03:52—> loader.php - _ci_load_class function - File Exists ‘/home/sites/SITENAME/application/libraries/MY_Smarty_parser.php’
    DEBUG - 2010-10-09 19:03:52—> loader.php - _ci_load_class function - NOT File Exists 3 ‘/usr/local/lib/codeigniter/current/system/libraries/Smarty_parser.php’
    ERROR - 2010-10-09 19:03:52—> Unable to load the requested class: Smarty_parser

    In this function - function _ci_load_class of Loader.php - I’ll quote a bit..

        // We’ll test for both lowercase and capitalized versions of the file name
          foreach (array(ucfirst($class), strtolower($class)) as $class)
          {


    log_message('debug', "loader.php - _ci_load_class function - Upper/Lower array '$class' ");


    $subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;

    log_message('debug', "loader.php - _ci_load_class function - SUBCLASS NOW '$subclass' ");


    // Is this a class extension request?
    if (file_exists($subclass))
    {


    log_message('debug', "loader.php - _ci_load_class function - File Exists '$subclass' ");

    $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
    if ( ! file_exists($baseclass))
    {
    log_message('debug', "loader.php - _ci_load_class function - NOT File Exists 3 '$baseclass' ");
    log_message('error', "Unable to load the requested class: ".$class);
    show_error("Unable to load the requested class: ".$class);
    }

    The thing is - it HAS found file :- /home/sites/SITENAME/application/libraries/MY_Smarty_parser.php
    But then /usr/local/lib/codeigniter/current/system/libraries/Smarty_parser.php is NOT found.


    Why test for both of them if its going to fail on the 2nd one anyway ??  Shouldn’t Codeigniter just test the 2nd one first & get it over with ?

    (I’ll copy it over & try again )

  • #14 / Oct 09, 2010 6:25am

    WanWizard

    4475 posts

    I think you are digging yourself in to the point where the pit is to deep to get out of.

    Stick to the way Codeigniter works, and don’t try to come up with strange setups, and then wonder why it doesn’t work. Do not touch anything in system. Do not install anything there.

    How you should install Smarty:
    - create a directory called ‘smarty’ in application/libraries. Install Smarty in there.
    - create a library called ‘smarty_parser’ in application/libraries:

    <?php
    require_once 'smarty/Smarty.php';
    
    class Smarty_parser extends Smarty
    {
        // do your thing here
    }

    - load it like a normal library and you’re in business.