0

I am trying to create a URL in the path format in yii but it always creates it in the get format. I do not understand whats going wrong.

this is the main.php

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

and this is the controller

class AirlineSearchController extends Controller
{
public function actionRoundTripSearch($origin)
       {
           echo $origin;   
       }

       public function actionLets()
       {
          echo $this->createUrl('roundTripSearch',array('origin'=>'delhi'));
       }
}

but it always results in /services/airlineSearch/roundTripSearch?origin=delhi
Question :- How can i get it in the path format?

2 Answers 2

1

I solved the problem.

'rules'=>array(
'airlineSearch/roundTripSearch/<origin:\w+>'=>'airlineSearch/roundTripSearch',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                ),

I just removed the <origin> from

'airlineSearch/roundTripSearch/<origin:\w+>'=>'airlineSearch/roundTripSearch/<origin>',

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

1 Comment

Nice answer. +1 for both Q & A :-)
0

I would always recommend removing the default Yii URL rules and add your own, specific ones. Also try using useStrictParsing. Both of these help to control your URLs more closely and will generate 404s as appropriate.

This would be my approach:

'urlManager'=>array(
    'showScriptName' => false,
    'urlFormat'=>'path',
    'useStrictParsing'=>true,
    'rules'=>array(

        'services/airline-search/<trip:round-trip|one-way>/<origin:\w+>' => 'airlineSearch/roundTripSearch',
    ),
),

And then in your controller:

<?php

class AirlineSearchController extends Controller
{
    public function actionRoundTripSearch($origin)
    {
        print_r($_GET); // Array ( [trip] => round-trip [origin] => delhi )

        // Use the full route as first param 'airlineSearch/roundTripSearch'
        // This may have been the cause of your issue
        echo $this->createUrl('airlineSearch/roundTripSearch',array('trip' => 'round-trip', 'origin'=>'delhi'));
        // echoes  /services/airline-search/round-trip/delhi
    }

    public function actionLets()
    {

    }


?>

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.