abasit83
abasit83

Reputation: 13

how to pass alphanumeric parameter in yii2 controller action

My code is

class WorkshopsController extends Controller
{
    public $layout = false;

    public function actionIndex($address)
    {
        echo $address;
        return $this->render('workshops.twig');
    }
} 

I want to pass any place address to above given action. how can i do that in Yii2. Address can be like "Mazyad Road, Mazyad - Al Ain - أبو ظبي - United Arab Emirates"

Upvotes: 0

Views: 581

Answers (1)

Tony
Tony

Reputation: 5867

You need to request you workshops/index page with address query parameter example.com/workshops/index?address=some address. Yii will automaticaly call your action and pass query params into it.

EDIT

If you wish to use pretty url, you must specify urlManager rules for it. E.g.

rules => [
    'workshops/<address>' => 'workshops/index',
],

Also if you wish your page open when addess parameter not specified you can define rules like this:

[
    'pattern' => 'workshops/<address>',
    'route' => 'workshops/index',
    'defaults' => ['address' => ''],
]

Upvotes: 1

Related Questions