Made code highlighted and fixed some indenting.

Davit Barbakadze 2013-07-25 09:03:11 -07:00
parent 2afd994f12
commit ffea933a31

@ -23,17 +23,15 @@ In any case, this explains why it's usually called the **MY_Controller** approac
So, create your file - **application/libraries/MY_Controller.php** - and fill it with this code:
```php
<?php
<?php
class MY_Controller extends Controller {
function MY_Controller () {
parent::Controller();
}
}
//...
}
```
Save and quit. Obviously this file doesn't actually *do anything useful* but we'll get back to that in a minute.
@ -43,36 +41,44 @@ Save and quit. Obviously this file doesn't actually *do anything useful* but we
We'll now modify one of your Controllers to work via the MY_Controller. The following code partial is obviously not a fully functional Controller - I'm just showing enough so you know what you have to change. Here we're using the Controller **Forum**. CodeIgniter encourages users to stick with the PHP4 (constructor name is the same as class name) syntax, but both formats are shown here.
```php
<?php
// Consider these lines in a normal PHP5 Controller:
class Forum extends Controller {
function __construct() {
parent::Controller();
}
}
//...
}
// If you are using PHP4 constructs, it will look like:
class Forum extends Controller {
function Forum() {
parent::Controller();
}
}
//...
}
```
Now, change the relevant lines to look like this:
```php
<?php
// Modify them to look like this instead (for PHP5):
class Forum extends MY_Controller {
function __construct() {
parent::MY_Controller();
}
}
//...
}
// Or, for PHP4 format:
class Forum extends MY_Controller {
function Forum() {
parent::MY_Controller();
}
}
//...
}
```
Now test your application - it will work exactly the same as it did previously. MY_Controller *is* being loaded - the extra processing time would be difficult to detect, so you'll just have to trust me here.
@ -90,14 +96,13 @@ Usually people are looking for a way of making functions accessible across all t
Edit your **application/libraries/MY_Controller.php** file again, and modify it to look like this:
```php
&lt;?php
<?php
class MY_Controller extends Controller {
function MY_Controller () {
parent::Controller();
}
}
/**
* Pretty date
@ -133,10 +138,10 @@ class MY_Controller extends Controller {
$output = substr ($date_in, 0, 10) ." ~ ". $hh_string;
return $output;
} // end-function pretty_date ()
} // end-class MY_Controller
} // end-function pretty_date ()
} // end-class MY_Controller
//...
```
## Using a MY_Controller method from your Controller
@ -146,13 +151,14 @@ The pretty_date() function above is particularly useful for ISO8601 timestamps t
Edit your functioning Controller and add in a reference to this function. I'll assume you're using $this->data for your view data, and that you have a view file you can use this in. You'll see that we simply use the **$this->** prefix, which will look in the parent controller (in this case MY_Controller) for a matching method or attribute.
```php
<?php
// somewhere in one of your application's Controller files
...
// somewhere in one of your application's Controller files
//...
$this->data['friendly_date'] = $this->pretty_date ("2009-05-31T16:42:07");
...
//...
$this->load->view ('your_view', $this->data);
...
//...
```