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

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.

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?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.