user1597438
user1597438

Reputation: 2231

YII2 - Linking actions from controller to html::button in views

I'm currently studying Yii 2.0 framework and I've stumbled upon a question. On a tutorial I've followed on the guide here.

In the controller, there's an action called actionEntry but there's no indication how the action knew when to be triggered.

I've tried creating a custom controller with an action called actionAdd. It will simply render a different view once it's called. Below is the snippet I have:

public function actionAdd() {
    return $this->redirect('entry', '');
}

Then on my view I've added a button using the html helper like so:

<?= Html::button('Add', ['class' => 'btn btn-primary', 'onclick' => 'actionAdd()']]) ?>

Honestly, I simply assumed that the button tag of the html helper would have an 'onclick' option because everything I've seen so far makes use of CHtml which I haven't gotten into just yet. This didn't work so my question is, how do you link controller actions to the view buttons? And how does the controller exactly know when to trigger the actions?

Note: Note sure if it helps, my button is in my index class.

Upvotes: 0

Views: 2365

Answers (1)

Tony
Tony

Reputation: 5867

Controller actions are not linked to buttons. You must specify form's action attribute. E.g. <form action="myController/myAction" ..other attributes..> to submit your form, and make a request to that action. The controller's action is called when you making a request to specific route e.g. http://examle.com/controller/action. Read about how yii handle requests in official yii2 guide

Upvotes: 1

Related Questions