Welcome Guest, Not a member yet? Register   Sign In
Consistent 404 after initial success
#1

[eluser]Wittner[/eluser]
Hi,

I've been enjoying learing CodIgniter. Today at some stage 'though, it all seemed to fall apart. I had been succesfully using a controller, a model which got some stuff from my db and a view to show it off. So far so good. Suddenly nothing seems to be working. I've done the simplest example of a controller without success (code below)

<?php
class Names extends Controller{
function index()
{
echo "Hello World";
}
}

?>

This gives me a 404 when I browse http://localhost/blah/index.php/names/
The name of the file is names.php. It is sitting in the 'controllers' subdirectory. Initially I had this running a view but when it wouldn't work I stripped it down to the basics you see above. Please help as I was enjoying CodIgniter but now I'm getting a bit concerned as I really want to use it for my next project,

cheers,

Wittner
#2

[eluser]Chris Williams[/eluser]
What are you reading from the log?

I'm curious to know since I'm getting a 404 as well. It works fine on my local server but as soon as I uploaded it all to my website, 404 all the way.
#3

[eluser]Wittner[/eluser]
Thanks Chris,

which logs? I have a logs folder but didn't see anywhere to turn them on. By the way, the example files 'welcome' and 'blog' still work fine!

(embarresed CodIgniter newbie face on)

;-)

Wittner
#4

[eluser]Chris Williams[/eluser]
If you have the log directory writable and turned on logging in the config file, you should see results for your pages.
#5

[eluser]mglinski[/eluser]
Wow, way to miss a critical piece of information. You need a class constructor for controlers and models:
Code:
<?php
class Names extends Controller{
function Names()
{
parent::Controller();
}

function index()
{
echo "Hello World";
}
}

?>
That should fix it.
-Matt
#6

[eluser]Wittner[/eluser]
Wow, way to make me feel stoopid dude! ;-)

I started everything from scratch and things are working again. Seriously, thanks for the reply. It brings up a related point I've been wondering about. I've noticed in the CodeIgniter video tutorials that the blog controller does not use the parent::Controller() constructor for the index controller. Some tutorials I've seen use it and others don't. Are there cases in which you don't use it, or are some tutorials just trying to cut down code for us newbies? The example I'm working with is working without the constructor so I'm a little confused,

cheers,

Wittner
#7

[eluser]Wittner[/eluser]
Chris: Sorry, just noticed your reply now. Didn't see an email notification for it. Thanks mate. I'll turn that on, especially while I'm still experimenting,

cheers,

Wittner
#8

[eluser]xwero[/eluser]
The constructor for both the controller and model extended classes aren't needed if you are not going to add something to the constructor in the extended class.

The extended classes inherit the constructor of the parent class. Once you add a constructor to the extended class, the parent class constructor gets overloaded. that is why you need to add the line;
Code:
parent::Controller()
This calls the parent constructor explicit so you are sure it gets executed.
#9

[eluser]Wittner[/eluser]
hmmm, thanks xwero.

Quote:...aren’t needed if you are not going to add something to the constructor

So was the code sample I first posted here syntactically correct?

Code:
<?php
class Names extends Controller{
    function index()
    {
        echo "Hello World";
    }
}

?>

All I did there was add an index() function. I wasn't adding any new construction. (by the way, I'm aware that echoing from the controller is bad practice, it's just for the test)

cheers,

Wittner
#10

[eluser]xwero[/eluser]
syntactically it's correct yes.

Echoing is considered bad practice for any function, if it's in a class or not but sometimes you need to echo data. For instance if you do an ajax manipulation of the database and all you need to know is if the query is executed or not, you can echo 0 or 1 to let the javascript code know the execution status. I think wrapping this value in a format, json or xml, is just a waste of bandwidth.




Theme © iAndrew 2016 - Forum software by © MyBB