Dynamic Pretty URL's in Yii2 using get Parameters - yii2

I am trying to generate dynamic URL's using yii2 routing, but I didnt find any proper example for what I am looking for
I have a page which has list of users. If I click on any user name it gets redirected to particular user's profile page.
The URL for profile page is like
https://www.example.com/frontend/web/users/profile?id=1&name=xyz
I want to show this URL as https://www.example.com/xyz where xyz is users name.
I have seen examples of pretty URL's but couldnt find any specific example.
How to such dynamic URL's. Please help.

You cannot fully remove static link from link. You can leave profile and remove other. Then your link will be https://www.example.com/profile/xyz
First of all add to config:
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
'profile/<name>' => 'users/profile',
...
],
Change your action:
public function actionProfile($name) {
$user= User::findOne(['name'=>$name]);

Related

yii2 formwizard checkbox field value set to 1 even if not selected

I am using yii2-formwizard and I want to insert a checkbox as form input field for the field is_legal in a tabular step. So in fieldConfig array, reading the documentation, I inserted the following code:
'is_legal' => [
'options' => [
'type' => 'checkbox',
'template' => '{input}{beginLabel}{labelTitle}{endLabel}{error}{hint}',
],
'labelOptions' => ['label' => \Yii::t('app', 'Legal Representative')],
],
If I select the checkbox or not the value of the field is always 1 as shown on: .
However, when I add another instance of the model, in the preview step I have NA as value of the legal representative field :
Yes, you are correct about it. It incorrectly shows the value even if the check box is not checked i have updated the section and added a fix.
//check if single checkbox input
if (inputType.attr("type") == 'checkbox') {
return inputType.is(":checked") ? inputType.val() : '';
}
To get the latest code you need to repeat the steps for running composer using,
composer update
and clear the browser cache along with clearing the assets folder in the web directory.
Normally when i am working locally with extensions or if there is an update for an extension which includes javascript updates i add the following settings under the components array in my local config file that takes care of getting the latest files from, and the assets are force copied every time to the web/assets/ directory whenever you refresh the browser or the page reloads.
'components'=>[
'assetManager' => [
'forceCopy' => true,
],
]
Note: Dont leave it open on live site as it would make the page load slower.

Yii2 internationalizating slug and controller

I'm making a yii2 site in 2 languages (for now) using yii's native i18n module, but how can I add multilanguage support for the action URLs?
For instance one of my actions is category/slug and in English, it will show as
http://example.com/category/chair
but in spanish it has to be shown as
http://example.com/categoria/silla
and so on for other languages shown in the future
right now im manually adding my routes like:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<alias:\w+>' => 'site/<alias>',
'marca/<slug>' => 'site/brand',
'categoria/<slug>' => 'site/category',
'brand/<slug>' => 'site/brand',
'category/<slug>' => 'site/category',
],
],
Do i need to manually route every action to its correct controller or is it possible to add a more automated form using yii::t() function?
you will need to write your own UrlRule class, implementing yii\web\UrlRuleInterface and configure your UrlManager.
read more here.
basically it is about "translating" a given request like '/categoria/silla' to your internal url ['site/category', 'slug' => $slug, 'lang' => $lang] in your UrlRule's 'parseRequest' method. 'createUrl' is the other way round.

Yii2 -- Pretty Url -- Domain to Controller/Action with parameters

What will be the rules for my pretty url if I have the following scenario:
links like this where parameters may vary.
domain/?bt=<token>&e=<email>
or
domain/?lt=<token>&e=<email>
then should be processed in a controller/action. ie. mycontroller/get
Also, parameters should be accessible by $_GET inside the action.
the simplest way is based on the use of urlHelper
use yii\helpers\Url;
$myUrl = Url::to(['your_controller/your_action', 'bt' => 123, 'e' => 'myemail#gmail.com']);
Using the urlHelper function Url::to .. the url you need is properly formed depending of the urlManager configuration you have set in your config file
and the param a manager as show in the sample like entry in an array.
The post or get method is related to the type of metho you have in your ulr call if not other values are specified the url is formed as a get
and you can obtain the values you need in $_GET['bt'] and $_get['e']
http://www.yiiframework.com/doc-2.0/yii-helpers-url.html
http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html
http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'' => 'call-backs/get',
'unsubscribes' => 'unsubscribes/get',
],
],
#scaisEdge, thank you for answering my question. maybe my question isn't that clear but this is the solution I made for my question after a hard find of clues and tips online.
All I wanted was that when a user clicks on a link, hitting the main page/main domain, it will go to my yii project (intended to be a webservice or an API like one) then will be handled by a precise controller and action.
'' => 'call-backs/get'
the code above answers the question. Cheers.

Yii2 get access only using pretty URLs

I'm using URL manager like the following:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'verses/view/<id:\d+>' => 'verses/view',
],
],
It works fine to make access using mysite.com/verses/view/158. The problem is, it is still possible to access the same content using non pretty URL i.e using plain get parameter such as mysite.com/verses/view?id=158. I need any way to restrict the access using the pretty URL.
I have tried the following couples of rules separately, but nothing I have gotten:
'verses/view<?id=>' => 'Error404',
'verses/view?id=<\d+>' => 'Error404',
What is the point of such restriction?
Anyway, one way to do it is something like this:
public function actionView($id)
{
if (strpos(\Yii::$app->request->getUrl(), '?') !== false) {
throw new \yii\web\BadRequestHttpException;
}
// ... the rest of action
}
No changes in UrlManager needed.
Try using UrlManager parameter enableStrictParsing = true.
What happens. UrlManager checks all rulls and they all do not match the request. Thus, by default it checks all default rules. Among default rules it finds the rule with ?id= and preforms routing to that one.
So, in order to avoid that route, you need to list all possible routes in the UrlManger rules and make enableStrictParsing = true. The routes not listed in the config rules parameter will be ignored.

Url rule overrides in Yii

I am just getting into Yii and so do excuse me if this looks blindingly obvious. I am trying to setup some URL rules so that:
www.example.com/my_page & www.example.com/my_page/my_subpage
both hit the main pages controller and the content pulled from a database, then:
www.example.com/admin/pages/edit
Works from the Admin Controller. I seem to have an either or situation at present, can anyone shed some light as to where I may be going wrong?
This is what I currently have in my web config file:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
//'<key>' => 'pages/index',
'admin/<action:\w+>' => 'admin/index',
],
],
If I toggle the commenting on either line 5 or 6 then it seems to work as I expect, however having both lines uncommented the '' line takes precedent.