Yii2 stripping ending slash in Url::to() route when using params - php

I'm using Yii2 and have been using the Url::to method without any issue. Now I just tried it with params for the first time and it strips the ending slash from my route.
So normally, say I would do this:
Url::to('/my/route/', true);
// http://www.example.com/my/route/
But if I want to pass params, such as:
Url::to('['/my/route/', 'id' => 123, 'name' => 'larry']', true);
// http://www.example.com/my/route?id=123&name=larry
Is there any way to stop it doing this, if not, do I have any other options to get what I want?
My UrlManager rule:
'rules' => [
[
'pattern' => 'foo/<name:[0-9a-zA-Z\-]+>.<some_id:\d+>',
'route' => 'foo/index',
'suffix' => '/',
],
//.........
],

1) You can achieve that by adding suffix to corresponding url rule containing that route:
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
[
'pattern' => 'my/route',
'route' => 'my/route',
'suffix' => '/',
],
],
],
],
2) Globally it's available through urlManager property with the same name suffix.
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'suffix' => '/',
],
],
3) Alternative way of setting through component:
Yii::$app->urlManager->suffix = '/';
This one works for specific routes too, just add this line before calling Url::to();

Related

YII2 sef urls construction

I have catalogue with products. Urls for products looks like this domain.com/adv?id=14792.
I want to beautify urls like this domain.com/adv/14792.
In web.php I tried to like this
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
'adv/<id:\d+>' => 'site/adv',
'<alias:[\w-]+>' => 'site/<alias>',
],
],
and there is no result.
I tried another variant in rules block:
'rules' => [
'adv/<id:\d+>' => 'adv',
'<alias:[\w-]+>' => 'site/<alias>',
],
and urls start to look like I want. But on this link I get 404 page.
Please use action with adv so that it work.
'rules' => [
'adv/<id:\d+>' => 'adv/<add some action here>',
'<alias:[\w-]+>' => 'site/<alias>',
],
I solved my problem by myself. This is my urlmanager module now:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'adv/<id:\d+>' => 'site/adv',
'<alias:[\w-]+>' => 'site/<alias>',
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
],
this construction build url from https://example.com/adv?id=123456 to https://example.com/adv/123456.
In view link build with
Html::a($insideEl,Url::to(['adv','id' => $ladvert->id]))
and in siteController action is
actionAdv($id)
Thanx.

How To allow access to default route in yii2 Advanced Template?

I need to give access to home page for full access with out login ex http://www.example.com/
but it works when only http://www.example.com/site/index because site/index is set as default route
how to give permission to site/index with out in url in yii2
'as beforeRequest' => [
'class' => 'yii\filters\AccessControl',
'rules' => [
[
'allow' => true,
'actions' => ['login','site/index'],
],
[
'allow' => true,
'roles' => ['#'],
],
],
'denyCallback' => function () {
return Yii::$app->response->redirect(['site/login']);
},
First of all you should include these lines in your config frontend/config/main.php in to components section:
...
'baseUrl' => '/',
...
'request' => [
//...
'baseUrl' => '',
//...
],
Next is to configure UrlManager to react on such request:
'rules' => [
'' => 'site/index',
//...
],
After that try to go to your http://your_url.local
It should work.

Yii2 rest api rewrite rules

This is how my url manager looks like:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'pluralize' => false,
'controller' => 'v1/user',
'extraPatterns' => [
'GET account' => 'account',
],
]
],
]
In order to make this call I can access this url:
localhost/project/api/web/v1/user/account
What I need to do is to remove controller name from url(user). I need to call the api at this url:
localhost/project/api/web/v1/account.
How can I achieve this? I tried a lot of tricks but none worked.
Thanks for your help.
You need to add following expression under rules
'v1/account' => 'v1/user/account',

How to do URL routing settings in Yii1?

I've problem with url in Yii.
when I enter URL like this,
http://localhost/yii_project/index.php/gii/default/login
it doesn't opens that page and it redirects to home page.
But when I enter url like this,
http://localhost/yii_project/index.php?r=gii/default/login
it open successfully.
I don't want to use index.php?r=. I want to display it index.php/gii/..
Change /protected/config/main.php as
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'search/<action:\w+>/'=>'dashboard/search/<action>',
'view/<action:\w+>/'=>'dashboard/view/<action>',
),
for more Info Hide index.php in yii
This is Yii1 or Yii2 you are using ?
For Yii2 Basic App (current version), open #app/config/web.php and add the following code:
This will result in this:
http://localhost/yii_project/gii/default/login
If you wanted the index.php included also then change a line to 'showScriptName' => true,
return [
... ,
'components' => [
.... ,
// below is the new section to include:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
//'suffix' => '.php',
//'cache' => 'cache',
//'scriptUrl' => '',
//'baseUrl' => '/',
//'hostInfo' => 'http://www.yourhost.com.au',
//'routeParam' => 'r',
'ruleConfig' => [
'class' => 'yii\web\UrlRule'
],
'rules' => array(
[
'pattern' => 'gii',
'route' => 'gii',
'suffix' => '',
],
[
'pattern' => '/<controller:\w+>/<action:\w+>',
'route' => 'gii/<controller>/<action>',
'suffix' => '',
],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
... ,
],
];

Yii2 if only id parameter is provided redirect to id plus slug

My current setup has the following URL structure
http://somedomain/blog/1/title
If the slug title part is wrong it will redirect to the correct slug however if you entered the following url you'd get a 404 (no slug provided):
http://somedomain/blog/1
Now I want to redirect the user to the proper URL with the slug, for example on stackoverflow if you enter https://stackoverflow.com/users/1917403 if will add my username to the end.
Url manager
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'blog/<id:\d+>/<slug:[-a-zA-Z 0-9]+>' => 'blog/view',
],
],
And here is my controller view action
public function actionView($id, $slug = null)
{
$model = $this->findModel($id);
$model->author_info = json_decode($model->author_info, true);
if ($slug != $model->slug) {
return $this->redirect(['blog/view', 'id' => $id, 'slug' => $model->slug]);
}
return $this->render('view', [
'model' => $model,
]);
}
The rule you created doesn't match since the slug is not found. It will simply bypass it.
So you can either use the defaults-functionality for the rule to make one of the parameters (slug in this case) optional, or just specify a second rule without the slug part.
Example for solution 1 would be:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
[
'pattern' => 'blog/<id:\d+>/<slug:[-a-zA-Z 0-9]+>',
'route' => 'blog/view',
'defaults' => ['slug' => null],
],
],
],

Categories