Welcome Guest, Not a member yet? Register   Sign In
Open Source CI Framework That Builds Pages From Reusable Components
#5

[eluser]solvo[/eluser]
Hey there boltsabre, thanks for the question.

Before I answer, let me just say that I'm a big believer in finding what works for you and going with that. So while I hope that my little framework is useful for you, if you have a way of doing things that works well for you, I completely support your doing it the way that you want/feel most comfortable with.

To answer your question, it sounds like your setting up individual forums to have their own controllers, which is why you have parameters inside of your controller to identify which forum that controller is for (please do correct me if I am wrong). My guess is that your URI segments are somewhat similar to "http://base_url/forum_name1" (or http://base_url/index.php/forum_name1" if you haven't done the whole modrewrite thing).

If I understand you correctly, if anything ever needs to change in that forum template you have, now you have 30 files that need to be updated appropriately.

I think on a theoretical level that's the biggest difference between what my framework does and the framework you've built does. I would have a single "Forum" controller with methods like "index", "view_forum", "view_topic", "post", and the parameters passed to those methods in the URI would take the place of the parameters you are hard-coding into each individual forum controller. Here's what the setup might look like:

Code:
class Forum extends Page {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $this->addComponent('ForumBreadcrumb');
        $this->addComponent('LoginForm');
        $this->addComponent('ListForums');
        $this->addComponent('ForumStats');
    }

    function view_forum($forum_id) {
        $this->addComponent('ForumBreadcrumb');
        $this->addComponent('LoginForm');
        $this->addComponent('ListTopics', 'content', array($forum_id)); //Can also get the forum_id from $this->uri->segment(x) in the component method
        $this->addComponent('ForumStats');
    }

    function view_topic($topic_id) {
        $this->addComponent('ForumBreadcrumb');
        $this->addComponent('LoginForm');
        $this->addComponent('ListPosts', 'content', array($topic_id)); //Can also get the topic_id from $this->uri->segment(x) in the component method
        $this->addComponent('ForumStats');
    }

    function post($topic_id) {
        $this->addComponent('ForumBreadcrumb');
        $this->addComponent('LoginForm');
        $this->addComponent('AddPost', 'content', array($topic_id)); //Can also get the topic_id from $this->uri->segment(x) in the component method
        $this->addComponent('ForumStats');
    }

    function submit_post() {
        //You could process this right here if you want to since you're processing POST data, and then redirect the user, or you can throw it to a component to handle. For me, personally, I try to keep my controllers clean, so I might write a component to do the processing, or just use that AddPost component from above and call a processing method inside of that. It doesn't make that much of a difference though.
        $this->addComponent('AddPost', 'content', array(), 'processPost');
    }

Obviously that's an overly simplified forum system right there, but it would work. The idea behind my framework is that it automatically handles the building of the page from your various components, allowing you to get straight into programming the functionality you want to program. Here that would be the breadcrumb, login form, stats info, and then the content specific to each page.

Also, the component class I wrote that you would extend for each of your components allows you to write your methods just like if you were working inside a controller, and not a library. Controllers have complete access to the CodeIgniter object on each page load view $this->, and your components will as well. This way you don't have to keep doing the whole $CI = &get;_instance(); deal.

If you wanted to do AJAX stuff and call specific pieces of a component, like re-rendering a component on the page, or doing a "more news" kind of thing, I've built functionality for that into the platform as well. There's a component that comes bundled called Cmpt that allows you to call a specific, public methods in a component through the URL. In the above code example, we could AJAX the post submission stuff by POSTing to http://base_url/cmpt/AddPost/topic_id_number. Then you can handle the response on the page without a reload. The platform will throw an error if someone tries to access a private method, so security is built right, you don't need to worry about that.

Also, I try to avoid helpers as a point of personal preference. I like classes with defined methods instead of freely callable functions, but that's not to say they don't have their place (again, this is solely my personal preference). I have however included a utilities helper in CodeByrner that has a function called dump() which will stop output on the page and give you diagnostic information on the variable you pass it. I find this really useful for debugging.

Think I've also begun to throw in support for a service layer, which I think is a major idea lacking in CI. Controllers are gate keepers, they call services which define business logic, and services go to models to get the information they need. Of course, if you don't want to do that, CodeByrner will work just fine without using services (and the service class comes bundled but turned off by default anyway).

So there's actually a whole lot of stuff going on in this little framework.

...all that being said, did I come even remotely close to answering your question? Please let me know.

Take care,
- JB


Messages In This Thread
Open Source CI Framework That Builds Pages From Reusable Components - by El Forum - 06-05-2012, 08:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB