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
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 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.
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,
[...]
)
I have been tasked to explore the possibilities of creating dynamic urls.
What we try to achieve is serving a domain for the client.
eg. www.example.com/clients/john/
eg. www.example.com/clients/bill/
These urls are not fixed, but have to be made dynamically everytime a Client registers at our site. Our URL's are build of Controllers and Actions. Like www.example.com/controller/action.
How can this be achieved in the PHP Zend Framework 2?
Big thanks in advance!
All you need is the ZF2 Manual:
I suggest to use segment routes
You can define them like:
'client' => array(
'route' => '/clients/:username',
'constraints' => array(
'username' => '[a-zA-Z][a-zA-Z0-9-]+', //accepted value pattern
),
'defaults' => array( //which action to invoke
'controller' => 'Application\Controller\ClientController',
'action' => 'showclient',
),
));
Then you can create an url with url helper like:
$this->url('client', array('username' => 'John'));
And in your controller you will be able to get username param with:
$this->params()->fromRoute('username');
I'm following the method described here to have my control panel in backend folder.
The problem is I don't want to create two assets folders so I want to set assetManager baseUrl property in my backend/config/main.php but I'm enable to get the URL of the backend in the config. I don't want it hard coded because I need the code to be portable.
What can I do?
Thanks.
You can configure asset manager's baseUrl in /backend/config/main.php
'components' => array(
...
'assetManager' => array(
'class' => 'CAssetManager',
'baseUrl' => 'frontend.assets'
),
...