Rosh
Rosh

Reputation: 491

How to pass value to controller through URL in YII framework of php?

This is the code

 <a type="button" class="btn btn-info" href="<?php echo CHtml::normalizeUrl(array(Yii::app()->controller->getId() . '/createStudent')); ?>">Add Student</a>

I want to pass class ID to the controller create student along with the url. The class id is in the variable

<?php $classId = $selectedSchoolClass->getId();?>

Upvotes: 0

Views: 985

Answers (2)

Tamas Kelemen
Tamas Kelemen

Reputation: 81

Use the class Html to create <a></a> tags.

<?= Html::a('Add Student', 
        ['createStudent', 'id' => $classId], 
        ['type' => 'button', 'class' => 'btn btn-info'] ) ?>

Upvotes: 0

JP. Aulet
JP. Aulet

Reputation: 4408

If I understand you, you have almost did it, you should only concatenate it with the & caracter and send the new param/data with the URL:

Yii::app()->controller->getId() . '/createStudent&classId='.$classId;

From the controller you can receive the data with:

$request = Yii::$app->request;
$classId = $request->get('classId');
// equivalent to: $classId = isset($_GET['classId']) ? $_GET['classId'] : null;

Hope it helps!

Upvotes: 1

Related Questions