I have an old website, now I have written it with new version of yii framework and I want change the urls, but because of SEO problems I want to keep my old urls.
Now when the user enters www.mysite.pre/car/details/10908 I want application renders www.mysite.pre/site/show_detail/10908 how could I handle it in yii2 Routing?
Assuming you have got this action in your SiteController class
public function actionShow_detail($id) {}
Add this in your configuration file:
// ...
'components' => [
// ...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
// ...
'car/details/<id:\d+>' => 'site/show_detail',
],
],
],
More details and information about Yii 2 routing can be found in "Routing and URL Creation" section of The Definitive Guide to Yii 2.0.
Related
i'm trying to implement mdmsoft/yii2-admin with advanced template with this pull request
but when i try add route to menu get error "Route "x" not found."
if i try force add route via database the menu show an empty array.
for this you need to create one inside config/main.php
'urlManager' => [
'enablePrettyUrl' => true,
'rules' => [
'test/' => site(ControllerName)/test(actionName),
]
];
Let me know was this helpful or not.
I have the following url manager path
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:(\w|-)+>/' => 'site/index',
'<module:api\w+>/<controller:\w+>/<action:(\w|-)+>' => '<module>/<controller>/<action>',
],
]
What am looking for is all urls not rendered via api module paths to run via site/index but all other paths having api/* to be executed via module paths.
The above works for urls like /login, /auth but when i run urls like
/administrative/uom
It fails
SO basically i want all urls to be redirrected via site/index but all urls having api as the prefix like api/auth/login to be run via their respective controllers.
I have added an api module which should handle this.
What else do i need to add to make this work?
When adding rules always start from more detailed to less detailed. And you added general rule for controller only so no URL with action is matched. Do this
'rules' => [
'api/<controller:\w+>/<action:[\w\-]+>' => 'api/<controller>/<action>',
'<controller:[\w\-]+>/<action:[\w\-]+>' => 'site/index',
'<controller:[\w\-]+>/' => 'site/index',
],
I'm trying to figure this out how to access other actions inside the DefaultController of a module. I have this module structure.
--module
--website
--module
--hub
hubmodule is a submodule inside the website module, I can access this url website/default/index no problem there, the problem is this website/hub/default/index its not accessible.
but with this website/hub/default it all good, but i can't have that. Tried a new action actionTest its inaccessible also. Here the config i have
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
// Default ruling for module/controller/action route to work
'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
],
],
I kinda solve my own problem with this, configuration
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
// Default ruling for module/controller/action route to work
'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
'<module:\w+>/<submodule:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<submodule>/<controller>/<action>',
],
],
format should be <module>/<submodule>/<controller>/<action> I didn't know that submodule was a reserve word in Yii URL config, but i still have this issue.
When you create an action like actionThisExample the url should be like this website/hub/default/this-example, but it goes to 404 page. When i do it like this actionThisexample this url website/hub/default/thisexample works. but its not that good to have url s like that in my case.
I'm using the Foundationize custom build of Yii2, which is a couple of years old but in general seems to run just fine.
However, when I've come to run the gii tool for generating new models etc. the routing is ignored and my application's index page is simply reloaded.
The URL I'm trying to access is http://localhost/web/index.php?r=gii
I have this line in my index.php
defined('YII_ENV') or define('YII_ENV', 'dev');
and my config/web.php contains the following
if (YII_ENV_DEV) {
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'allowedIPs' => [$_SERVER['REMOTE_ADDR']], // always allow on current server
];}
Am i missing a step somewhere? I tried the 'basic' setup from the main Yii site and that seemed to work fine on the same server. Thanks in advance.
For some reason it was the routing that was broken in this install
instead of
http://localhost/web/index.php?r=gii
I used
http://locahost/web/index.php/gii
and it was all fine
I think you modified your configure ( in config/web.php)
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager', //clase UrlManager
'showScriptName' => true, // to remove index.php
'enablePrettyUrl' => true //for Friendly url
],
],
If you use the advanced template, try
http://localhost/mysitename/frontend/web/index.php/gii
Or
http://localhost/mysitename/frontend/web/index.php?r=gii
I am new to yii2.
mydomain.com/organisation/banks/index
from above url i want to remove organisation.
i add following code to my config file under component section.
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>'=>'modules/<controller:\w+>/<action:\w+>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
],
]
But still its not working.
Is there any way to do it?
Thanks
In case of multiple URLs to hide I'm afraid you have to add the rule for each URL like:
'banks/index' => 'organisation/banks/index',
'something/another' => 'organisation/something/another',
If there is an unique controller's name that this module uses you can make it easier by adding general rule. For example if controller's name is banks and there is no BanksController under the top namespace level of app you can add:
'banks/<action>' => 'organisation/banks/<action>',
If organisation module is the only one your app uses and you don't need top namespace level controllers you can add:
'' => 'organisation',
So entering the domain URL address gets you directly to organisation module default route.