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.

Newbie Frustration

November 12, 2012 6:32pm

Subscribe [1]
  • #1 / Nov 12, 2012 6:32pm

    mikeklon

    8 posts

    My God, I must be stupid.

    I’ve been trying to use CI now for about 3 days.  I keep breaking it.  After I follow a tutorial, then try a second or third tutorial.  Something breaks (maybe routing).  Then if I try to go back and just see if I can launch index.php, I get the Error Was Encountered message.  Nothing is apparent, all the code looks fine.  Route configuration looks like the original.

    I end up “reinstalling” and starting over.  I’m sad to admit that I’m on iteration #5. 

    I’m ready to give up on this framework.  It’s either to easy to break or I’m just stupid. (May be the latter is true and I should just give up period - too old to learn new tricks LOL.)

    Everyone says the help files are just great, but I’m sorry, the help/tutorials for raw beginners leaves A LOT to be desired.  I have no idea where to turn.

  • #2 / Nov 12, 2012 6:43pm

    Aken

    2430 posts

    Do you have PHP experience to begin with? A framework is not a great place for someone who is new to the programming language entirely to start.

    You can also let us know what specific problems you’re having, including the code you’re using and the errors / results you’re getting, and someone can help explain what’s going on and how to fix it.

  • #3 / Nov 13, 2012 7:43am

    mikeklon

    8 posts

    Thanks for the reply Aken. 

    I have some PHP experience, not a lot.  I can get around.  I’m newer to OOP so that may be more of a weakness than PHP.  The OOP concept is not difficult, but the details of keeping track of who’s doing what when and inheriting what, when, why, etc. has been a bigger learning curve.  However, pressing on:

    I’m using version 2.1.3 of the User Guide Tutorial.

    The specific problem I’m having is in working with the tutorial.  I’ve worked through the first static pages part and everything seems to be OK on that. 

    I’m getting stymied on the second tutorial on the database query (I’m using a MySQL implementation).  The database, table, and user access are in place and work.  The models, controllers, and views are coded and placed exactly as shown in the tutorial. I can access the database and table info.  I’ve modified the default routes file as specified in the tutorial.  The first page (.../index.php/news) of the tutorial displays the stored table info as expected.  Also, when I hover over the “View article” link(s), I see the expected URI info for the table row to access.  However, when I click that link is where I get a 404 - Page not found message.

    During a diagnostic, I changed the route (in config/routes.php) for the route “news/(:any)” to a non-wild card destination, and then my browser does navigate to the specified page.  I’m not wondering if something isn’t “goofy” in the tutorial’s code somewhere so that the view method is not being invoked properly?  Maybe something changed during the last update that made the framework not like the tutorial syntax? 

    The code for application/views/news/view.php seems tenuous.  Also, the array selector ‘news_item’ seems to be undefined in the public function view(slug).

    I suspect it’s more likely that I’ve got something wrong.  My PHP debugger is not finding anything (Eclipse).

    Thanks again for any guidance.

  • #4 / Nov 13, 2012 7:48am

    Narf

    155 posts

    It would be easier if you could post your routes.php config and what you expect from each of its entries.

  • #5 / Nov 13, 2012 7:56am

    mikeklon

    8 posts

    The routes are exactly as specified in the tutorial.  Here they are for your convenience:

    $route['news/(:any)'] = 'news/view/$1';
    $route['news'] = 'news';
    $route['(:any)'] = 'pages/view/$1';
    $route['default_controller'] = 'pages/view';

    It seems the

    $route['news'] = 'news';
    works, but
    $route['news/(:any)'] = 'news/view/$1';

    is yielding 404 Page not found.

    What I was wondering is has anyone gone through this part of the tutorial successfully?


    I will post more code and file structure in one moment!

  • #6 / Nov 13, 2012 8:14am

    mikeklon

    8 posts

    Abbreviated File structure:

    Application:
      config:
        routes.php
      controllers:
        news.php
      models:
        news_model.php
      views:
        news:
          index.php
          view.php

    Files:

     

    news.php:

    <?php
    class News extends CI_Controller {
     
     public function __construct()
      {
      parent::__construct();
      $this->load->model('news_model');
      } // end function __construct()
      
     public function index()
      {
      $data['news'] = $this->news_model->get_news();
      $data['title'] = 'News archive';
      
      $this->load->view('templates/header', $data);
      $this->load->view('news/index', $data);
      $this->load->view('templates/footer');
      } // end function index()
      
     public function view($slug)
      {
      $data['news'] = $this->news_model->get_news($slug);
      
      if (empty($data['news_item']))
       {
       show_404();
       } // end if
      
      $data['title'] = $data['news_item']['title'];
      
      $this->load->view('templates/header', $data);
      $this->load->view('news/view', $data);
      $this->load->view('templates/footer');
      } // end function view()
      
     } // end class News

     

    index.php:

    <?php foreach ($news as $news_item): ?>
    
     <h2><?php echo $news_item['title']?><h2>
     <div id="main">
      <?php echo $news_item['text'] ?>
     </div>
     <a href="http://news/&lt?php"> ?>"]View article</a>
    
    <?php endforeach ?>


    view.php:

    <?php
    echo '<h2>'.$news_item['title'].'</h2><p>';<br />
    echo $news_item['text'];

     

    pages.php

    <?php
    
    class Pages extends CI_Controller {
    
     public function view($page = 'home')
      {
       if (!file_exists('application/views/pages/'.$page.'.php'))
       {  // Whoops, we don't have a page for that!
        show_404();
       } // end if
     
      $data['title'] = ucfirst($page); // Capitalize the first letter
     
      $this->load->view('templates/header', $data);
      $this->load->view('pages/'.$page, $data);
      $this->load->view('templates/footer', $data);
    
      } // end function view()
    
    } // end Class Pages

     

    news_model.php:

    <?php
    
    class News_model extends CI_Model {
     public function __construct()
      {
       $this->load->database(); 
      } //end function __construct()
     
     public function get_news($slug = FALSE)
      {
       if ($slug === FALSE)
        {
        $query = $this->db->get('news');
        return $query->result_array();
       }
     
      $query = $this->db->get_where('news', array('slug' => $slug));
      return $query->row_array();
      
      } // end function get_news()
     
    }
  • #7 / Nov 13, 2012 8:19am

    PhilTem

    872 posts

    Routes are crucial to correct ordering.

    1) Always ensure the “default_controller” and “404_override” precede any other routes
    2) Always remember: Routes are parsed from top to bottom with a first-match-first-serve policy. In your case it should work though.

    Now, do you have a view()-method in your news-controller? Just because your route seems valid does not mean you have a valid method to showing content 😉

    Try replacing

    $route['news/(:any)'] = 'news/views/$1';
    // with
    $route['news/.+'] = 'news/views/$1';

    and see if that changes anything (though I don’t expect it to).

    PS: I never did the tutorial so I can’t talk about it working or not ;P

  • #8 / Nov 13, 2012 8:39am

    mikeklon

    8 posts

    Thank you PhilTem for the suggestions.

    You were right, changing

    (:any)
    to
    .+

    had no effect.

    My default_controller and 404_override are first.  (I moved them just to experiment).
    I have gone through all of the User Guide general topics, thanks.

    It looks to me like there a view method exists (in news.php above).

    What I’m not sure about is the variable ‘news_item’ .  Did that get set somewhere or is it NULL?

  • #9 / Nov 13, 2012 9:26am

    Narf

    155 posts

    Changing (:any) to .+ could only break it - it removes the catching parenthesis and there will be no variable to pass.
    Another obvious thing (that makes no difference) is that the ‘news’ route is redudant, it will still work if you remove it.

    I don’t think that anything is wrong with the routes. It’s more likely this check in News::view() that causes the 404 error page to be shown:

    $data['news'] = $this->news_model->get_news($slug);
      
      if (empty($data['news_item']))
       {
       show_404();
       } // end if

    ... meaning that you just couldn’t find the item that was specified in the URL parameter.

  • #10 / Nov 13, 2012 9:28am

    mikeklon

    8 posts

    Well now.  I think I’ve made some progress.  The problem seems to stem from the arbitrary array selector ‘news_item’.

    Apparently, the array selector

    'news_item'
    in the controller news.php is not defined.  I changed it to be
    news

    and the system yielded expected behavior.  I also had to modify the selector name in the view.php file.

    On another note, the 404 Page Not Found error shows up if there are any ”  spaces ” in the Slug field.


    My question now is should I bother reporting this to the development team/documentation team?  Is it worth it and if so, who do I send it to? (Maybe they intended the error to force a learning situation? Kind of clumsy if so.)

    Thank you all for your patience with a newbie!


    p.s.  Thank you Narf, I did not see your post before posting the solution I found.

  • #11 / Nov 13, 2012 9:51am

    Narf

    155 posts

    You just did. 😊

    But the only problem with the tutorial that I see is that it makes it easy by first naming the key ‘news’ and then ‘news_item’ on the next step. This will be changed in the next version.

  • #12 / Nov 13, 2012 4:12pm

    mikeklon

    8 posts

    Thank you all for the help.  I’m going to continue on with the next tutorial and continue evaluating CI.

    I do have one final question here.  I’m still getting the 404-Page not found if any white space exists in the slug field.  The white space is being passed as   to the subsequent page and I think something is choking on that.  Any slick solutions for solving this (other than not allowing a white space in the slug field) ?

    Thanks so much.

  • #13 / Nov 13, 2012 4:49pm

    PhilTem

    872 posts

    Usually you don’t want to have whitespaces in URIs, that’s why there is   to replace whitespaces with a valid URI/ASCII character.

    However, I’m not quite sure though why it’s producing 404s for you.

  • #14 / Nov 13, 2012 10:49pm

    Aken

    2430 posts

    You’re probably not quite understanding the purpose of a “slug”, and what format it is expected to be in in the database. You should check out the next page of the tutorial regarding Creating News entries, and you’ll see how the slug is generated.

  • #15 / Nov 13, 2012 10:50pm

    Aken

    2430 posts

    Andrey, for what it’s worth, this has been a problem for new users before. I think the Create part of the tutorial should come before the View part.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases