Yii2 - Subdomain to module routing - php

I googled and searched here, but didnt find a solution... All that I found was subdomain to controller route... But thats not what I´m looking for...
So... here we go.
I have 2 subdomains:
sub-domain-a.site.com
sub-domain-b.site.com
And I have 2 modules:
module-a
module-b
I want Yii2 to redirect all requests to sub-domain-a to module-a and all requests to sub-domain-b to module-b...
so, if I have a controller in module-a, I just call:
sub-domain-a.site.com/controller-a-1
instead of
www.site.com/module-a/controller-a-1
same thing with sub-domain-b and module-b...
How can I accomplish this with Yii??
Thx!!

in config:
[
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'sub-domain-a.site.com/controller-a-1' => 'module-a/controller-a-1',
],
],
],
]

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.

Add language to url in Yii2

I have following url for example:
http://mywebsite.com/testing/test
And I want it to be something like this depending on the current language:
http://mywebsite.com/en/testing/test
I have language stored in: Yii::$app->language
I tried to modify my URLManager (/config/web.php), but it doesn't work for me.
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
'/login' => 'site/login',
'/logout' => 'site/logout',
Yii::$app->language.'/'.'<controller:\w+>s'=>'<controller>/index',
Yii::$app->language.'/'.'<controller:\w+>/<id:\d+>/<title:\w+>'=>'<controller>/view',
Yii::$app->language.'/'.'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
Yii::$app->language.'/'.'<controller:\w+>/<action:\w+>/<id:\d+>/*'=>'<controller>/<action>',
],
],
Can You help me?
I found an extension. Even if you don't want to use it, you can look into it’s code to get an idea how it works. (Changing patterns / routing / etc.)
https://github.com/codemix/yii2-localeurls

Yii2 module url modification

In my app I have a module located in:
/frontend/modules/reports/controllers/TheModule.php/
frontend/modules/reports/controllers/views/the-module/index.php
The url for this is
frontend/web/reports/the-module/index
How can I have the url like this:
frontend/web/the-module/index ?
Because if I navigate to the module url as it is now, all the links in the main menu (which is common throughout the site) will get a /reports in their url.
Note that I am using the default url manager.
Try this in frontend configuration:
return [
// ...
'components' => [
// ...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'the-module/<action>' => 'reports/the-module/<action>'
],
],
],
];

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',

urlmanager with modules not working in yii2.0

This regarding module creation inside backend folder.Example i have created a module name as "api". And also sccessfully created controller for that modules.
here is my urlmanager code:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'rules' => [
'<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',
'<module:\w+><controller:\w+>/<action:update|delete>/<id:\d+>' => '<module>/<controller>/<action>',
],
]
when i access url with respect to module as "api" , controller as "country" and action as "create"
http://local2host.com/bootstrap/backend/web/index.php/api/country/create
it showing 404 Not Found error
Where i am going wrong?
The second rule will never work, Also you do not have to define the rules and the normal ones will work. So this
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
will make this link /website.com/core/contact/index work just fine.
But I am not sure why your route is not catching the first rule... strange. It should.
Better late then never.
For make module works you need to append it in config:
'modules' => [
......
'modulename' => [
'class' => 'app\modules\modulename\Module',
],
......
],

Categories