Static pages again

hi auxbuss,

I’ve made everything like described above, and but I still doens’t get the ‘.’ in the ‘view’ param converted to a ‘/’.

The link is created by the following code:

echo CHtml::link(‘Testlink’, $this->createUrl(‘site/page’, array(‘view’ => ‘contact.team’)));

which generates:

http://www.mydomain.com/contact.team

and not:

http://www.mydomain.com/contact/team

I really don’t know waht’s going on there.

Do you have tested your set-up with themes?

I’ll try to find time tomorrow to knock together an example yii app.

And yup I use themes.

Okay, you can clone from here: git://github.com/auxbuss/yii-static-pages.git

I built the project set-by-step, so if you want you can reset to any commit and diff the changes.

For anyone reading this not au fait with git all you need to do is:




git clone git://github.com/auxbuss/yii-static-pages.git



This will create a clone in a new folder named yii-static-pages with HEAD being a fully working webapp with static pages. Try using "git gui" and gitk if the command line is a bit scary or you are a pointy-clicky.

More basic set-up info:

You’ll need to point the yii root index.php at the yii framework, wherever you put it.

You’ll also need to adjust the RewriteBase in the root .htaccess to your project root.

If you clone to your public webroot then you’ll need to give your webserver permissions to protected/runtime, say:




sudo chown www-data:www-data runtime



Ok, now I’ve checked your setup and see, that it only works if I create the links with the following code:

echo CHtml::link(‘Testlink’, array('contact.team));

but when I call:

echo CHtml::link(‘Testlink’, $this->createUrl(‘site/page’, array(‘view’ => ‘contact.team’)));

it still doens’t work. I do have to create the links like in the second example, because there are some other params to be added to the url.

I’m not sure why you are using the dots; the point of this exercise is to get rid of them!

I’m not a big user of createUrl(), but this works:




    echo CHtml::link("about via 'view/about'", $this->createUrl("view/about"));



But if you do this:




    echo CHtml::link("about via 'view/about'", $this->createUrl(("view/about"), array('blah'=>'splurge')));



then it tries to go to: /view/about/blah/splurge, which is not what you want.

I guess you need an additional rule for urlManager.

I don’t know that you guys are still watching this, but I’ve solved it. So others don’t have to spin their wheels to solve this relatively simple goal, here’s how it’s done.

First, the instructions above were helpful and got me started in the right direction. The problem I had when implemented was if the URL is generated in a CMenu widget, the view was urlencoded, so ‘this/that’ becomes ‘this%2Fthat’ and that mucks up the whole process.

I’ll put all the steps here in detail:

Static pages in subdirectories can be inserted by using ‘view’=>‘test.this’ in the url array, but when using urlmanager, it ends up being /this.that instead of /this/that.

Start by creating the static file in protected/site/pages/this/that.php and create a new menu link, except instead of putting this.that, change it to this/that




$this->widget('zii.widgets.CMenu',array(

			'items'=>array(

				...

				array('label'=>'test', 'url'=>array('/site/page', 'view'=>'this/that')),

				...

			));



You’ll need to have urlmanager configured to accomplish this:




		...

		'urlManager'=>array(

                        // we're going to override urlmanager

                        'class'=>'UrlManager',

			'urlFormat'=>'path',

                        ...

			'rules'=>array(

                                ...

                                'pages/<view:.*>'=>'site/page',

                                ...

                        ),

		),

		...



As stated earlier, create PagesView.php in protected/controllers:




<?php

class PagesView extends CViewAction {

    protected function resolveView($viewPath) {

        $viewPath = str_replace('/', '.', $viewPath);

        parent::resolveView($viewPath);

    }

}



Edit protected/controllers/SiteController.php changing the class of ‘page’ from CViewAction to PagesView

Now, copy CUrlManager.php from your framework/web/ directory into protected/components and rename it to UrlManager.php (the class we configured in the main.php file)

Edit the new UrlManager.php file.

  1. rename CUrlManager class to UrlManager

  2. Change the class extension for UrlManager from CApplicationComponent to CUrlManager

  3. rename CUrlRule class to UrlRule

  4. Change the class extension for UrlRule from CComponent to CUrlRule

  5. In the UrlManager class, remove ALL properties

  6. In the UrlManager class, remove ALL functions except createUrlRule

  7. Edit the createUrlRule function , replacing CUrlRule with UrlRule

  8. in the UrlRule class, remove ALL properties

  9. in the UrlRule class, remove ALL functions except createUrl

  10. Edit createUrl:

Find this code:




		foreach($this->params as $key=>$value)

		{

                        $tr["<$key>"]=urlencode($params[$key]);

			unset($params[$key]);

		}



Change it to this:




		foreach($this->params as $key=>$value)

		{

                        if($route == 'site/page') {

                            $tr["<$key>"]=$params[$key];

                        } else {

                            $tr["<$key>"]=urlencode($params[$key]);

                        }

			unset($params[$key]);

		}



BAM - you’re done

I’ve attached both UrlManager.php and PagesView.php which you can drop right into your protected/components directory.