2

i create new controller in yii look like this:

class LanguageController extends Controller
{

    public function actionSwitchLanguage($language)
    {
        Yii::app()->session = $language;
        $this->redirect(Yii::app()->request->urlReferrer);
    }
}

in views file, i create new url look like this:

<a href="<?php echo Yii::app()->controller->createUrl('Language/actionSwitchLanguage',array('language'=>'en')); ?>">English</a>

but when clicking the link, it appears error:

Error 404
The system is unable to find the requested action "actionswitchlanguage".

somebody can help me?

3 Answers 3

1

Try this,

<a href="<?php echo Yii::app()->controller->createUrl('Language/switchLanguage',array('language'=>'en')); ?>">English</a>
Sign up to request clarification or add additional context in comments.

Comments

0

try this code:

No need to add action in while creating url.

No need to mention controller name as you are creating url from controller object.

<a href="<?php echo Yii::app()->controller->createUrl('switchLanguage',array('language'=>'en')); ?>">English</a>

4 Comments

using: Yii::app()->createUrl('language/switchLanguage',array('language'=>'en')); then url return: localhost/newapp/language/switchLanguage?language=en. how to change url return look like this: localhost/newapp/language/switchLanguage/language/en
@Toàn Have you fixed your problem?
Then accept the solution which helped you fix the problem. This may be helpful for others.
0

If you are creating Url like this, I mean using controller

Yii::app()->controller->createUrl('switchLanguage',array('language'=>'en')); 

then you do not need to tell the createUrl about the controller name. just pass the action Name without the action keyword as kumar_v has said.


The above statement is similar to

$this->createUrl('switchLanguage',array('language'=>'en')); here the $this is the object of the current controller, so here also you do not need to tell createUrl() about the controller name

Note:- The view should be of the same controller.

But if you are creating urls like

Yii::app()->createUrl('language/switchLanguage',array('language'=>'en'));

then you need to tell the createUrl() method about the controller as well as action (but without action Keyword :) )

Update

In your config/main.php

Uncomment the following portion

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

1 Comment

using: Yii::app()->createUrl('language/switchLanguage',array('language'=>'en')); then url return: localhost/newapp/language/switchLanguage?language=en. how to change url return look like this: localhost/newapp/language/switchLanguage/language/en

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.