Reputation:
Basically, I began learning codeigniter today and bought a book on the subject. The book references version 1.7 and the version i am working with is 2.1.
There is an example controller in the book that looks like this:
<?php
class Start extends CI_Controller {
var $base;
var $css;
function Start(){
parent::CI_Controller();
$this->base = $this->config->item('base_url');
$this->css = $this->config->item('css');
}
function hello($name)
{
$data['css'] = $this->css;
$data['base'] = $this->base;
$data['mytitle'] = 'Welcome to this site';
$data['mytext'] = "Hello, $name, now we're getting dynamic!";
$this->load->view('testview', $data);
}
}
However it will only work when i remove the following function:
function Start(){
parent::CI_Controller();
$this->base = $this->config->item('base_url');
$this->css = $this->config->item('css');
}
Does anybody know why? If so, how do i call my css file from the config.
EDIT: The view:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Web Test Site</title>
<link rel="stylesheet" type="text/css" href="<?php echo $base . "/" . $css; ?>">
</head>
<body>
<h1><?php echo $mytitle; ?></h1>
<p class='test'> <?php echo $mytext; ?> </p>
</body>
</html>
Any help would be massively appreciated. Thanks!
Upvotes: 1
Views: 1229
Reputation: 15476
There have been a lot of changes - you should see the changelog if you wan't to know exactly what.
Without knowing exactly what your book tells you to do, I can only give you some pointers as to what to do to make your code work.
Firstly, when creating a controller in CI 2.1, you should write it with PHP5 OOP style - not the PHP4 OOP style as you are doing in your example.
A simple example:
<?php
class Start extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
?>
Now this is your basic controller which just echoes 'Hello World!' when accessing index.php/start
.
If you need to do something, get configuration variables or load some models/libraries before going to the index() method, you should create a __construct() method which is called when the Start
controller is instanciated. This __construct()
method needs to call the construct from CI_Controller
(the parent you are extending from). And you do that by typing parent::__construct();
in your own construct method:
<?php
class Start extends CI_Controller {
public function __construct()
{
parent::__construct();
//call models, variables, whatever here
}
public function index()
{
echo 'Hello World!';
}
}
?>
And that's basically what you need to know about making your controller work.
I don't know what 'call your css file from the config' means - I don't know what the css config item contains or what the book want's you to do with it.
Upvotes: 1