Question

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?

Était-ce utile?

La solution

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>',

Autres conseils

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()
    {

    }


?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top