Content management

Hi everyone,

I am pleased to be a new member of the Yii community. So far I was working with CodeIgniter, but I got curious about Yii. After a couple of days reading about it, and browsing the website (tutorials, wiki…) I discovered that Yii is way more extended and more feature rich in some regions then CI. I am not saying CI is bad, in fact I liked it very much. Anyways that was a little bit off topic. My problem is that I want to create a simple system where I could add pages and post to pages from an administrative system. However I have never done this, so far I hardcoded every page and content into them. That I want to simplify so users can add pages post etc. also. Since I have never done it I do not know the concept behind it, how one should achieve this, and how to achieve this with Yii. It does raise a couple of questions for me. There can be pages where I only want static content but not posts, and vica versa, and some pages could have more then just plain content for example image gallery and so on, these pages would be manually coded. And there is the problem that you have to automate creating controllers and views for the pages maybe even apply skins/themes to it. It seems very difficult to even understand the way to do it properly. I know it is not exactly a Yii framework question but if someone could take a little bit of time and explain me how it should work, what methods to use, what concepts to follow, that would be much appreciated. I do not wish to get a complete and working solution, I want to do it myself and meanwhile understand what I am doing. Thank you in advance.

In my home made blogging software I have posts and pages which belongs to Post and Page models.

Post has many Comment(s).

There’s not much of a difference between those two models, actually.

It doesn’t have to be complicated.

My pages have a ‘show_in_menu’ field and a ‘menu_order’ field which my Menu class uses to build the array.

Menu extends CMenu, and merely passes the built from database menu entry array to its parent.

Works alright.

What I’m suggesting is that you take the blog tutorial code and just take it from there.

You might end up rewriting it several times, but then you’re started.

What you want is easy to accomplish and is not framework specific.

NOTICE: I will give you an example from my mind, never tested .

For example, you can have a controller named pages.

In CI, you could add a _remap() method and every request coming to /page/blah-blah-blah.html was routed to the page controller in the index method.

In Yii, you can achieve the same, with missingAction() method( http://www.yiiframework.com/doc/api/1.1/CController/#missingAction-detail)

So, in CI you would have:




class Pages extends CI_Controller{

    

    public function __construct()

    {

        parent::__construct();

    }


   public function _remap()

   {

      $this->index();

   }

  

   public function index()

   {

       //process here based on your uri segments

       $url = $this->uri->segment(2);

   }

}



In Yii, things are almost the same :




class PagesController extends CController

{


    public function __construct()

    {

        parent::__construct();

    }

    

    public function missingAction()

    {

        $this->actionIndex();

    }


    public function actionIndex()

    {   

       //process here based on your $_GET array 

       $url = $this->getQuery('action');

    }

}



of course, you need to take a look on how Yii url mapping works, because it is a bit distinct than CI .

Now that you have your controller set up, you can extract from the database the page data based on the $_GET in Yii and on uri segments on CI .

You will do this within a model, for this you need to take a look on how yii sees the models, because once again, this is a bit distinct than CI .

In the end, you will end up with something like:




    public function actionIndex()

    {   

       $url = $this->getQuery('action');

       $model = new Pages;

       if(!$page = $model->get($url))

       {

           $this->redirect('a new location');

       }

       // continue here . 

    }



One you get that structure, you said that, there will be pages that will look distinct.

Well, this is not a problem, you can do something like :




    public function actionIndex()

    {   

       $url = $this->getQuery('action');

       $model = new Pages;

       if(!$page = $model->get($url))

       {

           $this->redirect('a new location');

       }

       $views_path = Yii::app()->basePath.'/protected/views/pages/' ;

       $page_template = 'page-id-'.$page->id.'.php' ;

       if(file_exists($views_path.$page_template))

       {

          $this->render('pages/'.$page_template,array('page'=>$page));

       }

       else

       {

          $this->render('pages/common-page',array('page'=>$page));

       }

    }



Your pages can be accessible via http://www.site.com/pages/seo-name-of-the-page.html

In the above way, you can apply a distinct style for each of your pages.

This would be the part where you manage the dynamic pages from database, for all the other static pages, maybe you can create a controller for each of them, it will help you keep the things more organzied at the start.

Hope it helped somehow, there might be some errors in the method names but you will get the base idea.

Also, like the above poster said, maybe you can start with the blog tutorial, like you did with CI .

Thank you both for the help, you got me a couple of ideas how to accomplish what I need. Tho I really need to understand Yii a bit more. It looks genious, but a lot more complex. Thank you again :)