CodeIgniter controllers, how to avoid repeating code?
Every page has dynamic sidebar (column) such as 10 recent articles. It show list in title.
I have to repeat same block of code in every method (action) in the controllers files.
Eg:
<?php
class Blog extends CI_Controller {
function index()
{
// Sidebar code block
//some code for index
}
}
class Signup extends CI_Controller {
functi开发者_如何学Goon index()
{
// Sidebar code block
//some code for index
}
function login()
{
// Sidebar code block
//some code for login
}
}
?>
In the view folder. I have a sidebar file
There must be a way to void repeating.
Maybe create a base class and put your function inside it?
<?php
class BaseClass extends CI_Controller {
function index()
{
// Sidebar code block
//some code for index
}
}
class Blog extends BaseClass { // Extend your classes from the base class
}
class Signup extends BaseClass {
function login()
{
// Sidebar code block
//some code for login
}
}
?>
what if you declare it in the constructor , or in one of your config files? http://codeigniter.com/user_guide/libraries/config.html
精彩评论