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
Related
I am building a RESTful API with Yii2 (advanced) and endpoints are working as expected apart from a custom one I need.
My urlManagerlooks like:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' =>
['class' => 'yii\rest\UrlRule', 'controller' => 'api/v1/step', 'pluralize' => false],
],
If I add a custom action into the StepController like so it will work just fine - Calling with http://example.com/api/v1/step/test
public function actionTest()
However if I want to pass an ID in through the path I will receive a 404 error - http://example.com/api/v1/step/test/1
public function actionTest($id)
Is there something I'm missing?
Edit: Adding notes that may help others.
My example above was simplified but what I wanted my URL to look like was like http://example.com/api/v1/step/test-by-foobar/1 with the called method to be public function actionTestByFoobar($id). However to get this to work you have to set the urlManager rule like the following which I didn't find obvious:
'api/v1/step/test-by-foobar/1' => 'api/v1/step/test-by-foobar',
Notice that the value is hyphenated, not in camel-case.
With your code you can pass an id like this :
http://example.com/api/v1/step/test?id=1
But if you want to do it like this:
http://example.com/api/v1/step/test/1
You should rewrite the url like below:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule',
'controller' => 'api/v1/step',
'pluralize' => false
],
/* You are missing this line below */
'api/v1/step/test/<id:\d+>' => 'api/v1/step/test'
]
],
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',
],
],
],
]
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>'
],
],
],
];
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',
],
......
],
I want a view from another module to be rendered and I use path '//modulename/foldername/viewname'(without .php), but when I run it I get "Controller can't find the view" error. What do I do wrong? It used to work before. My version of Yii is 1.1.15.
EDIT:
These is my urlManager:
'urlManager' => array(
'class' => 'yupe\components\urlManager\LangUrlManager',
'languageInPath' => true,
'langParam' => 'language',
'urlFormat' => 'path',
'showScriptName' => false,
'cacheID' => 'cache',
'rules' => array(
)
But how can it be related to my problem? Doesn't urlManager has nothing to do with view aliases?
give it the full path to that file:
$this->render('application.modules.modulename.foldername.viewname' , $arrayData);