2

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"

1
  • 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 Commented Dec 28, 2011 at 1:45

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.