开发者

CodeIgniter controllers, how to avoid repeating code?

开发者 https://www.devze.com 2023-02-12 16:27 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消