How to apply url in Yii framework? - php

I have a page (commerce.php), I want to give the link to the below-mentioned button, how I do that in Yii framework? My page path is frontend/views/site/commerce.php.
Below code is belong from index.php page
<button class="btn"><h5>Marketing Team</h5>

try the following
<?= \yii\helpers\Html::a(
'Marketing Team', ['site/market'], ['class' => 'btn btn-primary']
); ?>

Related

Q: Yii2 integrating Rbac and button discrimination

I'm developing a website in php with Yii2 and I have a problem with Rbac issue. I've followed the offical guide, I run the migrations and now I have in my db the four default tables which define my roles and permissions. Now I don't know how to integrate these roles in my project, I mean I would like to have some views only visible to users with specific permissions but can't understand the way to implement this.
I have also a problem with login, I don't know how to discriminate a button click.
login (view):
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button', 'value' => 'login']) ?>
<?= Html::submitButton('Register', ['class' => 'btn btn-primary', 'name' => 'register-button', 'value' => 'register']) ?>
</div>
</div>
SiteController:
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if (isset($_POST['submit']) && $_POST['submit']=='login') {
return $this->goBack();
}
if (isset($_POST['submit']) && $_POST['submit']=='register') {
return $this->render('register');
}
return $this->render('login', [
'model' => $model,
]);
}
I just need to render in a different views the user after the right button click. If Login button is clicked I want to be redirected in login view, if Register button is clicked, I want to be redirected in register view.
This seems to be a two-in-one question.
First, RBAC.
This is explained very well in the docs. You can use AccessControl to only allow certain actions to be accessed by a role or permission. If you need to show some content in a view based on a role or permission, use if(Yii::$app->user->can('permission_or_role)) echo "I can"; (docs).
Second, buttons
Check this link, the name of the button must be the name you check for in the controller (not login-button/register-button and check submit).

Php/laravel "link_to_route" to JQuery <a> tag

I have this in Laravel
{{ link_to_route('users.publications.create', 'text of link', ['users' => $user->username]) }}
What it does is create a "< a >" HTML tag to a view in the site link this:
<a href="http://kinbu.co/users/luis02lopez/publications/create >text of link</a>
I want to have the same result in JQuery but it uses the "users" in the link route as other user:
var action_call = $({{ link_to_route('users.publications.create', 'publica uno como intercambio',
['users' => $user->username], ['style' => 'color:blueviolet']) }}
});
body.append(action_call);
With which I have the next error: http://imgur.com/a/Lrmbg
Do I need to create other var with the users name?
If any idea will be a great help. Thanks
A solution for get the logged (and principal) user information is:
Auth::user()->username
This bring the data of the logged in user.

Yii2:link to related update page from different model

I have two models namely daily_ward_entry and discharge_note both the models are related by discharge_note.regn_number = daily_ward_entry.ipd_patient_id. I want to create a link from daily_ward_entry to update page on discharge_note.
I can't make out how to create such link.
I have tried like this, but it doesn't redirect at all.
<?= Html::a('Go to Discharge NOte',
['discharge-note/update', $dischargenote->regn_number =>$model->ipd_patient_id],
['class' => 'btn btn-primary']) ?>
your mistake is not correct send parameter. parameter send with key-value pairs. see doc
<?= Html::a('Go to Discharge NOte',
['discharge-note/update', 'id' => $model->ipd_patient_id],
['class' => 'btn btn-primary']) ?>

Yii2 Button with Parameter

I have this button in my Yii2 project:
Html::a('label', ['/attributes/index'], ['class'=>'btn btn-primary']) ;
The button is located in the page:
/site/view
Now what I want to do is pass a parameter when this button gets clicked to the attributes/index page from the site/view page.
To be more specific it is the ID that I want to pass of a particular record from a DB I am viewing.
Cheers.
You can pass parameters as key => value pairs after the route:
Html::a('label', ['/attributes/index', 'id' => $id], ['class'=>'btn btn-primary']) ;
See the Yii2 docs: http://www.yiiframework.com/doc-2.0/yii-helpers-baseurl.html#toRoute()-detail

Yii 2 Exclude NavBar in Modal

In my index.php view, I have a button, once clicked, a modal will pop up:
<p>
<?= Html::button(
'Create New BizAdmin',
['value' => Url::to(['createbizadmin']),
'class' => 'btn btn-success',
'id' => 'modalButton'
]) ?>
<?php
Modal::begin(['id' => 'modal']);
echo "<div id='modalContent'></div>";
Modal::end();
?>
</p>
My modal file is createbizadmin.php where it has the following codes:
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $model app\models\User */
$this->title = 'Create New Bizadmin';
?>
<div class="user-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_formbizadmin', [
'model1' => $model1,
'model2' => $model2,
]) ?>
</div>
My problem is this:
As you can see, the navbar looks horrible. The menu list seems to overflow outside the modal.
How do I get rid of the navbar inside my modal? I can't seem to find which part of creatbizadmin.php I should edit.
Any thoughts?
I'm guessing that you have a controller somewhere that is handling the url createbizadmin. I'm also guessing that inside that controller action you are rendering the view file like this;
$this->render("createbizadmin");
If so, then that is your problem. By calling a view file directly, Yii will apply default layouts to the view file. You have no control over how this happens from within the called view file, and all your menus etc will be rendered.
To get around this you will probably need to render a partial file. This rendres the file without applying layouts. So use;
$this->renderPartial("createbizadmin")
Alternatively, if the modal content is being generated as a result of an ajax call, you can respond from the controller with;
$this->renderAjax("createbizadmin")
This article seems to have a good explanation of how best to achieve this; Render a form in a modal popup

Categories