How to hide id from url in yii2 - url

I'm trying to hide blog id from url.
For understanding better:
I want this URL
mysite/blog/post-slug-<id> // will be like: mysite/blog/post-slug-358
change to
mysite/blog/post-slug
Here my my URL code:
<a href="<?= Url::to(['post/view',
'id' => $model->id, 'slug' => $model->slug ]) ?>">
and this is my config/main:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'blog/<slug>-<id>' => 'blog/view',
]
could any one help me to solve it?

Remove -<id> from your url manager:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'blog/<slug>' => 'blog/view',
]
]
Then in your BlogController in actionView you can get it with this code:
$slug = Yii::$app->getRequest()->getQueryParam('slug');
I'm not sure that 'class' is nessecary; This i in my project how I use it:
'urlManager' => [
'baseUrl' => '/',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'ajax/<action>' => 'ajax/<action>',
'<first_step>/<second_step>/<third_step>' => 'page/index',
'<first_step>/<second_step>' => 'page/index',
'<first_step>' => 'page/index',
'<first_step:.+/>' => 'page/index', // redirect 301 /
],
],

Related

Ruby - Unable to append data to existing hash

I'm getting error no implicit conversion of String into Integer (TypeError) when I'm trying to push new_rule to existing hash.
Any idea what the issue is here?
require 'json'
network_sg_existing_rules = network_sg_properties["securityRules"]
puts network_sg_existing_rules
[{"name"=>"Port_8080", "id"=>"/subscriptions/44a91bb8-d388-467e-89e6-123456/resourceGroups/cloud-shell-storage-centralindia/providers/Microsoft.Network/networkSecurityGroups/sg_testing_temp_1/securityRules/Port_8080", "etag"=>"W/\"0d87997d-2a2a-4725-9be5-123456\"", "type"=>"Microsoft.Network/networkSecurityGroups/securityRules", "properties"=>{"provisioningState"=>"Succeeded", "protocol"=>"*", "sourcePortRange"=>"*", "destinationPortRange"=>"8080", "sourceAddressPrefix"=>"*", "destinationAddressPrefix"=>"*", "access"=>"Allow", "priority"=>100, "direction"=>"Inbound", "sourcePortRanges"=>[], "destinationPortRanges"=>[], "sourceAddressPrefixes"=>[], "destinationAddressPrefixes"=>[]}}]
# Append new rule.
new_rule = {
:name => "rule_4",
:properties => {
:protocol => "TCP",
:sourceAddressPrefix => "*",
:destinationAddressPrefix => "*",
:access => "Allow",
:destinationPortRange => "22",
:sourcePortRange => "*",
:priority => "301",
:direction => "Inbound"
}
}
network_sg_new_rules = network_sg_existing_rules["securityRules"].push(new_rule)
puts network_sg_new_rules
network_sg_properties is a hash which has a key called securityRules.
you are trying to add it in value of securityRules.
Instead of
network_sg_new_rules = network_sg_existing_rules["securityRules"].push(new_rule)
try
network_sg_properties = network_sg_properties["securityRules"].push(new_rule)
Hence value of network_sg_properties[securityRules] will be
[
[0] {
"name" => "Port_8080",
"id" => "/subscriptions/44a91bb8-d388-467e-89e6-123456/resourceGroups/cloud-shell-storage-centralindia/providers/Microsoft.Network/networkSecurityGroups/sg_testing_temp_1/securityRules/Port_8080",
"etag" => "W/\"0d87997d-2a2a-4725-9be5-123456\"",
"type" => "Microsoft.Network/networkSecurityGroups/securityRules",
"properties" => {
"provisioningState" => "Succeeded",
"protocol" => "*",
"sourcePortRange" => "*",
"destinationPortRange" => "8080",
"sourceAddressPrefix" => "*",
"destinationAddressPrefix" => "*",
"access" => "Allow",
"priority" => 100,
"direction" => "Inbound",
"sourcePortRanges" => [],
"destinationPortRanges" => [],
"sourceAddressPrefixes" => [],
"destinationAddressPrefixes" => []
}
},
[1] {
:name => "rule_4",
:properties => {
:protocol => "TCP",
:sourceAddressPrefix => "*",
:destinationAddressPrefix => "*",
:access => "Allow",
:destinationPortRange => "22",
:sourcePortRange => "*",
:priority => "301",
:direction => "Inbound"
}
}

How do I display "Loading..." message in Highcharts using only the yii2 extension?

I'm rendering Highcharts on my page with Pjax.
My view code:
<?php Pjax::begin() ?>
<?php ActiveForm::begin() ?>
//some code there...
<?= Html::submitButton( 'Calculate', [
'class' => 'btn btn-primary',
'id' => 'calculate-button',
]);
ActiveForm::end();
?>
<?php
if ($data) {
echo $this->render('HolidayChart', ['data' => $data]);
}
?>
Highcharts widget:
echo Highcharts::widget([
'scripts' => [
'highcharts-more'
],
'htmlOptions' => ['id' => 'holidayChart-id'],
'options' => [
'chart' => [
'type' => 'columnrange',
'inverted' => true,
'zoomType' => 'y',
],
'title' => ['text' => 'Chart'],
'inverted' => true,
'yAxis' => [
'title' => [],
'type' => 'datetime',
'dateTimeLabelFormats' =>
[
'day' => '%d.%m.%Y',
],
'min' => 1514764800000,
'max' => 1546300800000
],
'xAxis' => [
'title' => ['text' => Names'],
'categories' => $data['users'],
],
'series' => [
[
'name' => 'Name',
'data' => $data['data'],
]
]
]
]);
Controller code:
$calculateForm = new CalculateHolidayForm();
$data = '';
if ($calculateForm->load(Yii::$app->request->post()) && $calculateForm->validate()) {
$data = UserHoliday::calculateHoliday(UserSubgroup::findOne($calculateForm->subgroup), $calculateForm->dateRange);
}
return $this->render('about', ['calculateForm' => $calculateForm, 'data' => $data]);
I have two questions:
1. How can I display "Loading..." message before the rendering chart?
2. How can I display "Loading..." message before update data on the chart using only the extension? (About like this https://jsfiddle.net/hLj1advd/ )
Thanks a lot!

Zend Framework - routing same routes to different controller

I have two routes and want to match both routes when some parameter exists in request.
Route 1:
'companies' => [
'type' => Segment::class,
'options' => [
'route' => '/api/v1/companies[/:id]',
'defaults' => [
'controller' => V1\Rest\Controller\CompaniesController::class,
]
],
'priority' => 2,
'may_terminate' => true,
],
Route 2:
'company_members' => [
'type' => Segment::class,
'options' => [
'route' => '/api/v1/companies[/:id][/:members][/:member_id]',
'defaults' => [
'controller' => V1\Rest\Controller\CompanyMembersController::class,
]
],
'priority' => 2,
'may_terminate' => true,
],
I want to use CompanyMembersController when members exists in the request and CompaniesController when members doesnt exists .But it is not working.
Your issue is in the second route where you defined members as a parameter [/:members]. You should change this to a /members.
I also would recommend to use constraints for your route parameters. Your routes should look like:
'companies' => [
'type' => Segment::class,
'options' => [
'route' => '/api/v1/companies[/:id]',
'defaults' => [
'controller' => Controller\CompaniesController::class,
'action' => 'index',
],
'constraints' => [
'id' => '\d+'
]
],
'priority' => 2,
'may_terminate' => true,
],
'company_members' => [
'type' => Segment::class,
'options' => [
'route' => '/api/v1/companies[/:id]/members[/:member_id]',
'defaults' => [
'controller' => Controller\CompanyMembersController::class,
'action' => 'index',
],
'constraints' => [
'id' => '\d+',
'member_id' => '\d+',
]
],
'priority' => 2,
'may_terminate' => true,
],
Also you can see the constraints to parameters id & member_id to integers.
References
zend mvc routing
understanding routing
routing and controllers

Incorporate custom validation error messages into form object by element

I have the following code which creates a specific text element:
$this->add([
'type' => 'text',
'name' => 'newpassword',
'attributes' => [
'id' => 'newpassword',
'class' => 'form-control'
],
'options' => [
'label' => 'Enter New User Password',
],
]);
And I have the following code which produces my input filter definitions:
$inputFilter->add([
'name' => 'newpassword',
'required' => true,
'filters' => [
['name' => 'StringTrim'],
['name' => 'StripTags']
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 6,
'max' => 256
],
]
],
]);
What I want to accomplish is adding my custom messages. Here's the way they have it in the api documentation:
$validator = new Zend\Validator\StringLength(array('min' => 8, 'max' => 12));
$validator->setMessages( array(
Zend\Validator\StringLength::TOO_SHORT =>
'The string \'%value%\' is too short',
Zend\Validator\StringLength::TOO_LONG =>
'The string \'%value%\' is too long'
));
How do I incorporate my custom validation messages into my already programmed code?
UPDATE:
I think this is where i will find success, but not sure how to do it:
$inputFilter->get('newpassword')->getValidatorChain()->
Use this-: its messageTemplates to set custom message
$inputFilter->add([
'name' => 'newpassword',
'required' => true,
'filters' => [
['name' => 'StringTrim'],
['name' => 'StripTags']
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 6,
'max' => 256,
'messageTemplates'=>array(
\Zend\Validator\StringLength::TOO_SHORT =>
'The string \'%value%\' is too short',
\Zend\Validator\StringLength::TOO_LONG =>
'The string \'%value%\' is too long'
)
],
]
],
]);

ZF2 segment route with file extension

I have a route to an action which returns a pdf file. Now it would be fine if the url would contain the file extension .pdf. If the last part of my route is not a segment, it should work but not in my case.
Works
'my_route' => [
'type' => 'segment',
'may_terminate' => true,
'options' => [
'route' => '/my-file.pdf',
'defaults' => [
'action' => 'file'
]
]
],
Does not work
'my_route' => [
'type' => 'segment',
'may_terminate' => true,
'options' => [
'route' => '/my-file/:year.pdf',
'constraints' => [
'year' => '\d{4}'
],
'defaults' => [
'action' => 'file',
'year' => date('Y')
]
]
],
This is an example using Regex route (not fully tested):
'my_route' => [
'type' => 'regex',
'may_terminate' => true,
'options' => [
'route' => '/my-file/(?<year>\d{4}).pdf?',
'constraints' => [
'defaults' => [
'action' => 'file',
'year' => date('Y')
]
]
],
See the docs : https://framework.zend.com/manual/2.4/en/modules/zend.mvc.routing.html#zend-mvc-router-http-regex
You can adjust it to this:
'route' => '/my-file/:year:.pdf',
Note the extra : after the parameter

Resources