How to hide module name from url yii2 - php

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.

Related

mdmsoft/yii2-admin routes not working advanced template

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.

react with yii urlManager rule to a specific url

I have a noob question, but googling for several hours didn't get me closer to the answer.
I want that when I enter the following url into my browser, that is gets assigned to a specific controller action.
Imagine I have the following url:
http://localhost:5555/tractor-unit/?param1=1&param2=abc
How should the rules looks like to process this request in a certain action in a certain controller.
What I've tried so far:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'tractor-unit/?param1=<param1:\d+>&param2=<param2:\w+>' => 'tractor/do'
]
],
That doesn't work. I just don't understand how the rules entry should be written in order to pass this request to a controller named tractor and its action do. Or maybe I totally misunderstood the whole url manager concept.
simple add to action "do" in tractor-unit controller next:
public function actionDo($param1=null, $param2 = null)
if this don't work
remove section "rules" from config

Yii2 urlmanager rules for pretty urls fails

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

use static url in url routing yii2

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.

Yii framework: Controller url & parameters

In my application I have "PController" with default action "actionIndex", so in Yii the path becomes p OR p/index. Now in order to get certain users info I use the following path p/index?u=test where test is the username and u part of the path is basically a GET parameter (p/index?u=test).
Is there any way to do the same thing without index?u= part of the path, i.e. I want my path to look like example.com/p/test ?
In your application config find section for components and add options for urlManager
array(
......
'components' => array(
......
'urlManager'=>array(
'showScriptName' => false,
'urlFormat' => 'path',
'rules' => array(
'/p/<u:\w+>'=>'p/index'
),
),
),
);
http://www.yiiframework.com/doc/guide/1.1/en/topics.url

Categories