Welcome Guest, Not a member yet? Register   Sign In
Newbie and CodeIgniter Examples
#1

Good afternoon everyone.  I am in the process of building a new website for my company and I am now working with CI in order to have a solid framework that will allow me to keep my site secure.  It'll be a subscription site, but I plan to use a payment company's API to process my subscriptions as I want nothing to do with payments or the certifications it requires.

So, in reading about security I found that most recommendations was to use a framework to build your site, which most have built-in security for secure sites.  I looked at CakePHP and I couldn't get it to work nicely on my system, which then led me down the road to Zend.  While installing Zend using a tutorial for MAMP and OSX, the author recommended a more lightweight framework for newbies to the M-V-C world, which is me.  So, I decided to look at CI and I was impressed with how easy I was able to get it up and running on my system.  Minimal headaches!

I have been working my way through the tutorials and completed the tutorial on the news site and am now working my way through the General Topics and have run into a problem.  The controller example had me create a controller named blog.php.  I copied the code from the example and pasted it into my editor (TextWrangler) and then saved the file to my application/controllers folder.  For clarity my site is http://localhost:8888/codeigniter/.  I am getting a 404 page not found error when I go to http://localhost:8888/codeigniter/index.php/blog/.  The strange thing is that http://localhost:8888/codeigniter/index.php/news/ works fine.  The permissions and ownership are identical on both blog.php and news.php in the controllers folder.

Can anyone elaborate as to why I get this error with blog.php and not news.php?

The example codes is:
Code:
<?php
    class Blog extends CI_Controller {

        public function index() {
            echo 'Hello World!';
        } // Index
    } // Blog
?>

The one difference in this example that I noticed is the closing "?>" at the bottom of this controller whereas the other examples do not contain this.  I tried removing it and the results do not change. The apache log does not show any errors either.

Thank you for any help.

Tom
Reply
#2

Okay, I was reading another post about using CI3, so I downloaded and installed that in my MAMP server. I completed the tutorial with the same outcomes and then began working on the general topics, specifically the controller, and recreated my Blog.php controller file.

When I visited my page http://localhost:888/codeigniter3/index.php/blog it still gives me the 404 page not found error. However, unlike in my previous problem with CI2, I can get the "Hello World!" to display from Blog.php by using URL http://localhost:8888/codeigniter3/index.php/blog/index.

Does this have something to do with routes.php possibly? I left it as-is after the last edit in the tutorial.
Reply
#3

Quite possibly, yes.

index() is the method that should be called by default for every controller. If it's not, then you've probably changed it via routes.
Reply
#4

I'm not sure why CI isn't loading the index() function automatically. It's supposed to when no function is specified in the URL. I've only worked with 2.x, so could you post the routes in the routes.php file under your CI 2 installation? Just the routes without all the comments, of course.

I seem to remember an error in the tutorial that led to the problem you describe, but I can't remember the details. For what it's worth, once I got past that, it was smooth sailing, so hang in there!  Smile
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#5

(02-04-2015, 05:55 PM)RobertSF Wrote: I'm not sure why CI isn't loading the index() function automatically. It's supposed to when no function is specified in the URL. I've only worked with 2.x, so could you post the routes in the routes.php file under your CI 2 installation? Just the routes without all the comments, of course.

I seem to remember an error in the tutorial that led to the problem you describe, but I can't remember the details. For what it's worth, once I got past that, it was smooth sailing, so hang in there!  Smile

I appreciate your reply and insight to this problem.  Here's is the section of the file you requested.

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

CI3
Code:
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'Blog';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

If in CI3, I cannot use http://localhost:8888/codeigniter3/index.php/blog to get the index page to display.  I must use http://localhost:8888/codeigniter3/index.php/blog/index to get the index page to display. I still have not luck in CI2 to get it to display using any form of url.
Reply
#6

(This post was last modified: 02-05-2015, 01:40 AM by RobertSF.)

(02-04-2015, 06:41 PM)tcarroll2 Wrote: CI2
Code:
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

The problem is the 4th line, the one before the default controller. That route looks at your http://localhost:8888/codeigniter/index.php/blog/ and redirects it to http://localhost:8888/codeigniter/index..../view/blog, and since there's no such thing, you get a 404.

Routes are a powerful feature, but you generally don't need them, especially when building a simple application to gain experience. You could delete all those lines, and your application would still work as expected. That is, CI would route to the URL segments normally.

You know how those segments work? CI decides what code to execute based on your URL, using the following scheme: http://localhost/codeigniter/controller/...argument_2. So if your URL is http://localhost/codeigniter/blog/new_entry, CI will try to execute a function called new_entry() in your blog controller.

I would suggest you delete all routes except the default controller, and pick a controller to be the default. The default controller is the one you don't want to specify in the URL. For example, if you made blog the default controller, then your URL could be http://localhost/codeigniter/add_entry, and CI would know to look in the blog controller. For other kinds of pages, like news or products, you don't need routes. Your URLs would look like http://localhost/codeigniter/news and http://localhost/codeigniter/products.

What kind of documentation do you have? Here's a PDF of the docs for 2.1.2.
http://www.clicketyhome.com/codeigniter_..._guide.pdf

Note that in the URLs above, I've removed the main index.php file. That's not done with routes but with htaccess. The documentation explains how.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB