2
votes

I'm learning Yii and got into url routing problem. I have a controller as follows

class PageController extends Controller
{
public function actionIndex()
{

    echo 'index';
}
    public function actionGetPage($page = '')
{
            echo $page;
}

and in config/main.php

    'urlManager'=>array(
        'urlFormat'=>'path',
        'rules'=>array(
                            'page'=>'page/index',
                            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        ),
    ),

How can i set url rules so when i use http://localhost/page/About it should work and print "About"

2
try adding 'page/<action:(contact|license|about)\w+>' => 'page/getPage', to the bottom of your rule and perhaps reading yiiframework.com/doc/guide/1.1/en/topics.url - T I

2 Answers

8
votes

On way to do it: In your route configuration, you should have something like:

'page/<key>' => 'page/index',

And define actionIndex() as follows:

public function actionIndex($key) {
  echo $key;
...

Note the extra parameter required by actionView... . That will be equal to the used in the URL.

0
votes

Are you trying to make an easy function that handles all pages? Or just to get the About page to work?

If you do this:

public function actionAbout() {
   // code goes here
}

The view that gets called in actionAbout will be accessible at localhost/page/about

The page controller is what makes the /page/ work and the /about is defined by the name of your action. So anything after the action becomes the name in the URL. Hence:

public function actionTestingThis() {
    // code goes here
}

will be accessible at /page/testingThis Yii does camelcase and so the first T in TestingThis gets lowercased.

Does that answer your question?