I have removed the index.php from the URL using the .htaccess also the controller name "site" using the URL Management present in main/config.php.
But when I echo the base url(Yii::app()->baseUrl); it prints me with the index.php. Could anyone advise me on how to remove it?
In the UrlManager set the showScriptName to false and urlFormat to path:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
[...]
)
Related
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.
After uncomment enablePrettyUrl get 404 error
For example:
The requested URL /site/index was not found on this server.
Apache/2.4.7 (Ubuntu) Server at yiibasic.com Port 80
When I comment it again, everything is working
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
]
Can't understand what the problem is.
.htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
The (Yii2) URL manager supports two URL formats:
the default URL format;
the pretty URL format.
By default 'enablePrettyUrl' is set to false this mean that the url have the format
your/index.php?r=post%2Fview&id=100
If you uncomment 'enablePrettyUrl' => true,
'components' => [
'urlManager' => [
//'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
]
you enable the format for prettyUrl like:
/index.php/post/100
like as you easy see the two format are different so an explict call based on a format return a 404 error .. when you change the urlManager format enabling or disabling pretty url ..
you can call the url avoiding this problem using urlHelper
use yii\helpers\Url;
echo Url::to(['post/view', 'id' => 100]);
with this helper Yii2 create the proper url according on the pretty url enabling state in use
You can find a brief guide on yii2 routing here http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html
After adding .htaccess file in your ./web folder (if using basic template ), you must then add some rules in the urlManager section of your web.php config file...see below
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
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.
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