How to use hyphen/dash in a url? - php

I have this button in the _form.php file.
'buttons' => [
'replace' => function($url, $model) {
return Html::a(
'Replace',
[
'/european-countries/replace',
'countryIdReplacement' => '123' // Problem here.
]
);
},
],
My controller:
public function actionReplace($countryIdReplacement) {
// My code.
}
The URL is:
http://my-site/european-countries/replace?countryIdReplacement=123
I want to use this variable format:
http://my-site/european-countries/replace?country-id-replacement=123
But PHP doesn't allow to use that format. This is not allowed:
actionReplace($country-id-replacement)

Related

rewriting url with updating url in yii2

how change this:
site/faq
to this:
site/faq?festival=nouroz98&id=100000&data=information
this is the FestivalRule class
class FestivalRule implements UrlRuleInterface
{
public $pattern;
public $route;
public function createUrl($manager, $route, $params)
{
return [$route, $params];
}
public function parseRequest($manager, $request)
{
if(!empty($request->getQueryParams())){
if($request->getPathInfo() == 'site/faq'){
return [
'site/faq',
$request->getQueryParams()
];
}
}else{
return [
'site/faq',
[
'festival' => 'nouroze99',
'id' => '10000',
'data' => 'from db',
]
];
}
return false;
}
}
this is the urlmanager config:
'rules' => [
[
'class' => 'app\components\FestivalRule',
'pattern' => 'site/faq/<festival:\w+>/<id:\d+>/<data:\w+>',
'route' => 'site/faq',
],
I want if the URL requested without any parameters i changing that with parameters but i can't place the parameters in URL.
all parameters sent but i wanted url changes too (parameters shows in URL)!

Missing Required Parameter in Yii2 Gridview Action Button

I'm trying to add my own action button in Yii2-Kartik Gridview.
This is my custom button:
This is my code in index.php
[
'class' => 'yii\grid\ActionColumn',
'template' => '{edit}',
'buttons' => [
'edit' => function ($url, $model) {
return Html::a('<button type="button" class="btn btn-edit-npwp"><i class="glyphicon glyphicon-plus-sign"></i></button>', $url, [
'title' => Yii::t('app', 'Edit'),
'data-toggle' => "modal",
'data-target' => "#myModal",
'data-method' => 'post',
]);
},
],
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'edit') {
$url = Url::toRoute(['vatout-faktur-out/add-data', 'id' => $model->fakturId], ['data-method' => 'post',]);
return $url;
}
}
],
and this is my action in controller.php
public function actionAddData($id) {
$model = new VatoutFakturOut();
return $this->render('addData', [
'model' => $model,
]);
}
I want to process the data from the row that I've clicked the button.
But, this return error
Missing required parameters: id
Why does this happen? And how to fix this?
Thanks
In urlCreator you used if statement to check, if action is edit, and if it is, you add param id to your button url. Otherwise it doesnt have one, so there's two solutions:
Remove if statement from:
'urlCreator' => function ($action, $model, $key, $index) {
$url = Url::toRoute(['vatout-faktur-out/add-data', 'id' => $model->fakturId]);
return $url;
}
Or remove$id from your actionAddData() - because youre not using it at all:
public function actionAddData() {
$model = new VatoutFakturOut();
return $this->render('addData', [
'model' => $model,
]);
}

Yii2 - Change Language

I have a problem with my advanced template when I try to change page using internationalization.
I try to explain. To use the internationalization on my site I have follow this steps:
In params.php, I have added this:
<?php
return [
'adminEmail' => 'admin#example.com',
'languages' => [
'en' => 'English',
'it' => 'Italian',
],
];
I have added this in my frontend\views\layouts\main.php to call on my website the languages insert above:
<?php
foreach(Yii::$app->params['languages'] as $key => $language){
echo '<span class="language" id ="'.$key.'">'.$language.' | </span>';
}
?>
after I have created a new file named main.js. To permit to yii2 to see the main.js file I added this in the AppAsset.php (is it correct?). In this file I insert this:
$(function(){
$(document).on('click','.language', function(){
var lang = $(this).attr('id');
$.post('index.php?r=site/language', {'lang':lang},function(data){
location.reload();
});
});
$(document).on('click','.fc-day', function(){
var date = $(this).attr('data-date');
$get('index.php?r=event/create',{'date':date},function(data){
$('#modal').modal(show)
.find('#modalContent')
.html(data);
});
});
$('#modalButton').click(function(){
$('#modal').modal(show)
.find('#modalContent')
.load($(this).attr('value'));
});
});
after that in the sitecontroller.php I have added this:
public function actionLanguage()
{
if(isset($_POST['lang'])){
Yii::$app->language = $_POST['lang'];
$cookie = new Yii\web\cookie([
'name'=>'lang',
'value'=>$_POST['lang']
]);
Yii::$app->getResponse()->getCookies()->add($cookie);
}
}
after in config\main.php I have added this in components:
'components' => [
'i18n' => [
'translations' => [
'app' => [
'class' => 'yii\i18n\PhpMessageSource',
//'basepath' => #app/messages,
'sourceLanguage' => 'it',
'fileMap' => [
'app' => 'app.php',
'app/error' => 'error.php',
],
],
],
],
finally i have create on the root a folder called messages\it and messages\en and inside two files (one per folder) called app.php I have insert this text:
<?php
return [
'Benvenuto' => 'Welcome'
];
that's all...but after load my home page (correctly) when i click on one of the languages (Italian or English) I see the following messages:
jquery.js?ver=1.11.3:4 POST http://localhost/mdf/frontend/web/index.php?r=site/language 400 (Bad Request)
And If I try to paste directly this url, I obtain only a blank page:
http://localhost/mdf/frontend/web/index.php?r=site/language
This is only a partial response to your question
The blank page when you invoke using browser could be related to the fact that you don't render nothings in this case.
try manage the ajax this way
public function actionLanguage()
{
if (Yii::$app->request->isAjax) {
$data = Yii::$app->request->post();
if(isset($data['lang'])) {
Yii::$app->language = $data['lang'];
$cookie = new Yii\web\cookie([
'name'=>'lang',
'value'=>$_POST['lang']
]);
Yii::$app->getResponse()->getCookies()->add($cookie);
return;
}
} else {
return $this->render('index', []);
}
}

Yii2: configure yii\web\ViewAction::$viewPrefix

I want to change default directory ("pages") of static pages views, using yii\web\ViewAction. Documentation says:
You may configure yii\web\ViewAction::$viewPrefix to change the
directory for searching these views.
Question: where and how need I to change this property? I tried to do it in controller:
use yii\web\ViewAction;
...
public function actions()
{
(new ViewAction)->viewPrefix = ''; //wanted to do it just as '#app/views/site/', without any subdirectory ('pages' or others)
return [
'stat' => [
'class' => 'yii\web\ViewAction',
],
];
}
But no success.
Try to set viewPrefix to null
public function actions()
{
return [
'stat' => [
'class' => 'yii\web\ViewAction',
'viewPrefix' => null,
],
];
}

yii2:data of related model in Gridview

I have two models namely MedicineRequestEntry and MedicineRequest. MedicineRequestEntry is related to MedicineRequest via
public function getMedicineRequests()
{
return $this->hasMany(MedicineRequest::className(),
['medicine_request_entry_id' => 'id']);
}
Now in grid-view of MedicineReuestEntry I am trying to pull the data from MedicineRequest Model using the relation using two alternative ways
like
[
'attribute' => 'is_delivered',
'value'=> 'medicineRequests.is_delivered'
],
In this method I am getting the value as not set.
and another method:
[
'attribute' => 'is_delivered',
'value'=> '$data->medicineRequests->is_delivered'
],
In this method I am getting the error like:
Getting unknown property: app\models\MedicineRequestEntry::$data->medicineRequests->is_delivered
Now I need some help, what I am doing wrong here.
Thank you.
You should use a callback function, see the guide:
[
'value' => function ($data) {
$str = '';
foreach($data->medicineRequests as $request) {
$str .= $request->is_delivered.',';
}
return $str;
},
],
Or for the first result of the array:
[
'value' => function ($data) {
return $data->medicineRequests[0]->is_delivered;
},
],

Categories