0

I have had a good play around with the Yii Framework and now I want to take it a bit deeper and what I want to do is set up an application where several different URLs would point to the same controller.

Normally domain.com/content will point to class ContentController which is standard in MVC.

What I want to do is set up three controllers (maybe more but this will do to start), i.e. ArticlesController, DisplayController and SplashController.

I would then set up what is essentially a CMS for a client, and they would be able to create as many pages as they want and point them to the above three controllers, which I have already set up to handle the data.

So for instance my client could set up the following pages: news, notices, technical and have them all pointed to the ArticlesController, and also set up pages: management, specials, support and have them all pointed to the DisplayController.

I know that all those controllers could be creating using the Gii module, but in this case its not an option as I don't think that's suitable for non technical people.

I just want my client to be able to log into the CMS, decide he wants to create a new page called "randompage", point it to the ArticlesController using a drop down menu, then write a bunch of articles for it and now have those articles accessible at domain.com/randompage/article-1 domain.com/randompage/article-2

With the standard set up that would point to site/error because there is no controller RandompageController

What I've done so far is create a constructor in Controller class where I can overwrite the controller id

class Controller extends CController {
    function __construct($id) {
        // Code here which successfully pulls from the database
        // which controller the current page should point to.
        parent::__construct($newControllerID)
    }
}

If I check in the CController class, $this->_id = either articles, display or splash but the application itself stills loads site/error

I'm guessing I must have to set/override the Controller elsewhere. I have tried

Yii::app()->setController($newControllerID)

but that doesn't have any effect

Perhaps Yii is set up and must require a specific controller for each URL but that would mean developing rather rigid solutions for clients, and requiring them to call in the developer every time they want to add a new controller.

Hope I have explained what I'm trying to do well.

tereško
  • 58,060
  • 25
  • 98
  • 150
mr mojo risin
  • 555
  • 4
  • 15
  • tl;dr. No, no, in Yii it is simple to access same controller from different urls, just setup your urlmanager. –  Jul 05 '13 at 17:39
  • @PeterM he is trying to do the opposite of what you said. He is trying to use one url to access 3 different Controllers. – Pitchinnate Jul 05 '13 at 18:26
  • How would the program know whether '/articles/update/1' is a News, Notice or Technical object? Are all of these the same Model or do you have a different model for all three? If they are all the same model why would you want three different controllers? – Pitchinnate Jul 05 '13 at 18:31
  • @Pitchinnate piece of content in the database would belong to a page (or multiple pages), and every page would belong to one of the "display" controllers (in my example above articles, display or splash). From the Yii forum I have discovered the custom URL classes which can query a database to direct urls. Not sure how I missed it last night when I was reading the documentation although it was 2am :) – mr mojo risin Jul 05 '13 at 23:59
  • @mrmojorisin could you link that forum post? –  Jul 08 '13 at 08:50

1 Answers1

0
  • create a page object (id, title, slug, description, image)
  • create an article object (id, pageId, title, slug, content)

configure your urlManager:

'<pageSlug>/<articleSlug>' => 'article/view'

in your ArticleController->ViewAction evaluate

  • Yii::app()->request->getParam('pageSlug')
  • Yii::app()->request->getParam('articleSlug')

to find the right article for that page

  • You don't have to use lengthy `Yii::app()->request->getParam('pageSlug')` just use `$_GET['pageSlug']` –  Jul 08 '13 at 09:06