CodeIgniter Forums

Full Version: Absolute "noob" questions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4

El Forum

[eluser]callumd[/eluser]
Hi there,

I've been developing in PHP and MySQL for almost 3 years, and really enjoy it.

I've never used a framework, nor have I been able to conceptually understand what the benefit of using them is (despite asking numerous people).

However, there's no denying their explosive popularity, so I've decided to jump in and try and "learn" one, to see if I can grasp what these supposed benefits are.

When learning something, I find it difficult to just "press on" when I hit something I don't really understand, and that's what's happened, which brings me here.

I'm working my way through the official CodeIgniter user manual, and have come across two things so far, that I would really like some answers on, if I may.

Number one:

The user manual gives a tip on how to remove the need to have 'index.php' appear in every URI, by suggesting the following:

Quote:By default, the index.php file will be included in your URLs:
example.com/index.php/news/article/my_article

You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Doesn't work for me. When I create a .htaccess file and put just that bit of code in it, I get an error no matter what I am trying to load: "Object not found! The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again."

Second question:

In the "Views" section of the manual, the following code appears:

Quote:<?php
class Blog extends Controller {

function index()
{
$this->load->view('blogview');
}
}
?>

Specifically, this bit:
Quote:$this->load->view('blogview');
I've never seen OO PHP structured like this (granted, my OO experience is limited). "$this" obviously refers to the instance of the object, "view('blogview')" obviously refers to a method called "view" and 'blogview' is the parameter.

But what the heck is 'load'? I've never seen OO PHP written this way before.

Thanks in advance.

El Forum

[eluser]callumd[/eluser]
Another question:

In Controller.php, the class definition begins with:

Quote:class Controller extends CI_Base {
I'd like to have a look at the CI_Base class, but I can't find it.

Where is it?

Thanks.

El Forum

[eluser]callumd[/eluser]
And another:

I'm seeing a bit of stuff like this:

Quote:class Blogmodel extends Model {

var $title = '';
var $content = '';
var $date = '';

function Blogmodel()
{
// Call the Model constructor
parent::Model();

}

..where some class (that extends some other class) will call the parent constructor.

Are parent constructors not automatically initiated if I create an instance of class B if it extends class A?

El Forum

[eluser]Pascal Kriete[/eluser]
Hey callumd,

I'll move from the bottom up. Yes, constructors are automatically called if you don't define your own - it's a personal choice to put the 'emtpy' ones in.

CodeIgniter operates on one 'super-object'. Controllers extend this super object so you don't run into scoping issues.
Since inheritance in PHP 4 is essentially broken, a different base object is used for PHP 5. So if you open up system/codeigniter/base#.php, you will find your elusive CI_Base class.

Of course an application is more than just controllers. Libraries and helpers are used for shared code. CodeIgniter loads libraries as singletons (one instance) and then creates a class variable reference to that object, so you end up with $this->library_name . The loader is referenced with $load, so $this->load->view() calls the view function of the class in system/loader.php .

To get access to the current super-object in a library you can use the get_instance() function.

htaccess is not available on all server configurations, and it can be a pain to make it work if your host has imposed funny restrictions. Search the forums, you will find a lot of threads on this - many with very useful information.

Hope that covers most of it.
Welcome to CodeIgniter.

El Forum

[eluser]callumd[/eluser]
Thanks for the response, inparo.

One clarification:

So 'load' is an object contained within the 'controller' object, and 'view' is a method within 'load'?

If so, where can I see the 'load' class?

Incidentally, is there an easy way to locate class definitions?

Thanks again.

El Forum

[eluser]Pascal Kriete[/eluser]
Load is a reference to an instance of the loader class.
For a few core libraries this reference is created in system/libraries/controller.php:
Code:
$classes = array(
                            'config'    => 'Config',
                            'input'        => 'Input',
                            'benchmark'    => 'Benchmark',
                            'uri'        => 'URI',
                            'output'    => 'Output',
                            'lang'        => 'Language',
                            'router'    => 'Router'
                            );
        
foreach ($classes as $var => $class)
{
    $this->$var =& load_class($class);
}

In most cases the reference has the same name as the library.
So $this->router, is found in system/libraries/router.php . The loader is the only one that breaks this convention, because it makes more sense to write $this->load->whatever('name') .

As for locating them.
All libraries are in system/libraries. The core is found in system/codeigniter . The database stuff is in system/database.
Your own stuff should be declared in the application folder - you can overwrite or extend libraries as described here.

A good thing to do might be to step through codeigniter/codeigniter.php, which is where the magic happens.

El Forum

[eluser]callumd[/eluser]
Thanks.

Another question.

The user manual says:

Quote:all PHP files should OMIT the closing PHP tag, and instead use a comment block to mark the end of file and it's location relative to the application root

The same manual gives a sample controller for a Blog class:

Quote:<?php
class Blog extends Controller {

function index()
{
echo 'Hello World!';
}
}
?>
.. complete with closing PHP tag.

Are there some instances in which the closing tag should be included?

El Forum

[eluser]dmiden[/eluser]
I think that code snippet is just outdated. But I don't use the comment block to mark the end of a view file, just libraries, controllers and models.

El Forum

[eluser]callumd[/eluser]
Thanks!

Another:

I can't seem to get my application to connect to the database, the browser keeps showing this error message:

Quote:A Database Error Occurred

Unable to connect to your database server using the provided settings.
I can't see what's wrong with the settings.

How do I get CodeIgniter to show the specific error text?

I have error_reporting(E_ALL) at the top of my index.php page, but the browser still won't show the error text.

**EDIT** I was able to "fix" this by changing the $db['default']['dbdriver'] setting from "mysqli" to "mysql".

How come mysqli won't work? Is it not supported? All my other XAMPP applications (not written in frameworks) work with mysqli.

El Forum

[eluser]Randy Casburn[/eluser]
Hi callumd,

It's possible (if using CI 1.7.0) that you've not set the $db['default']['port'] = '3306'; Try that setting and see if mysqli works.

Some of the documentation has not completely caught up with the latest release, or the release has not caught up with itself. One or the other.

Hope that helps,

Randy
Pages: 1 2 3 4