Yii2 change url structure - php

Instead of have:
http://www.example.com/affiliate?ref=123456546546
I prefer to have
http://www.example.com/affiliate/ref/123456546546
Is it possible to pass GET param without changing .htaccess and keep my desired structure ?

try to add an rule to your UrlManager I guess your controller is affiliate and your action index
'urlManager' => [
...
'rules' => [
'<controller:(affiliate)>/ref/<ref:\d+>' => '<controller>/index',
]
...
]
I guess if you set custom rules you also have to add the following so that the default url's are still working.
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

Related

Yii2 does not find action

'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>/<action:\w+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
Not it is clear why references of a /controller/name-action/id/1 do work, /controller/1/name-action do not work, and without hyphen everything works, according to documentation of action name-action it is actionNameAction??
public function actionNameAction($id) {
// some code
}
Inline Actions
In advance all thanks.
\w does not include -.
Change pattern to [\w\-]+.
I think you are confused with zend framework and Yii2.In yii2 it is actionActionName.While using it in url's the capitals are changed to lower case with a hyphen preceding them.
For example,
if the controller is Orders
and the action is OrderAnalysis
then the url would be something like orders/order-analysis.
Moreover any id or any other parameter is added only after routing the app to its correct controller action.
Also now coming to your problem I think i found a backdoor to it-
// creates an anchored URL: /index.php?r=post%2Fview&id=100#content --------------------------
echo Url::to(['post/view', 'id' => 100, '#' => 'content']);

Yii2 get 404 page from action whith several words

Action name in my controller is actionMyEvents. When I try mysite.com/mycontroller/my-events I get 404 not found. Please help!
This is not the normal behavior of Yii Framework. In the default configuration, you have to call yout action like this:
mysite.com?r=mycontroller/my-events
To use clean URL like this, you have to configure your web server (probably Apache) rewrite engine, and do something like this in your config file:
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],

Yii routing module problems

This is my module "auth"
-auth/
-controllers/
---AuthController.php
in controller I have action "register"
In layout I create route url:
$this->createUrl('auth/auth/register')
Rules are in config/main.php
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'register' => 'auth/auth/register',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
),
When I'm in mySite/index.php register url looks well: mySite/register. Url works and view is visible. When I'm this view mySite/register here starts a problem. Register url is mySite/auth/auth/auth/register - of course i see 404 error.
Why it apeears and how to solve this problem?
From the API page for createUrl
If the controller belongs to a module, the module ID will be prefixed
to the route. (If you do not want the module ID prefix, the route
should start with a slash '/'.)
Thus your code should read
$this->createUrl('/auth/auth/register')

yii urlmanager parameter trouble

I'm a newbie of Yii and now I have a link:
localhost/qr/delete/33
I dont' know how to make it as
localhost/qr-code/delete/33
using urlManager in main.php.
Should be able to add this to your array in your config:
'qr-code/<action:\w+>/<id:\d+>' => 'qr/<action>',
//other rules here
This will make it so that delete or update actions can work. Make sure you put this before other rules so that it has a higher priority. id will be passed as a variable to your action in your qr controller
public function actionDelete($id) {
//your code here
}
If qr is controller , delete is action and 33 is id, then here is your solution .
rules = array(
'<controller:(qr)>-code/delete/<id:\d+>'=>'<controller>/delete',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),

Displaying complex outputs

I'm using Yii as a framework and I'm having trouble with this piece of code.
I want to output the apartments which has a "status" of "occupied"
So I have this CHtml link with querystring parameters
<?php echo CHtml::link('Occupied Apartments', array('/apartments/','status'=>'occupied')); ?>
But it still displays ALL outputs even if the status is "available". What must be the problem?
Any help would be appreciated
I think you are trying to construct the link as
http://yourdomain.com/controller/action/status/xyzStatus
For this, First you have configure this URL structure in urlManager in your config file ie, main.php in protected/config/
Just add bellow line in urlManager rules
'<controller:\w+>/<action:\w+>/<status:([A-Za-z0-9-]+)>' => '<controller>/<action>',
Finally your basic url rules will appear as
'rules' => array(
'<controller:\w+>/<action:\w+>/<status:([A-Za-z0-9-]+)>' => '<controller>/<action>', //This is newly added line
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
Now your url will work. You can get the status value with $_GET['status'];
Please filter your data provider with $_GET['status']; mostly on conditions with CActiveDataProvider .

Categories