yii2 url manager is not parsing second parameter - php

I try to setup some rest controller for products managing. I want action to ne run from url: /product/decrease/4/8
in url manager i have:
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:\d+>/<id2:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
)
I have error like this:
<response><name>Bad Request</name><message>Missing required parameters: decrease</message><code>0</code><status>400</status><type>yii\web\BadRequestHttpException</type></response>
What am I doing wrong?

Your Rules look ok, however I do not believe this rule
'<controller:\w+>/<action:\w+>/<id:\d+>/<id2:\d+>' => '<controller>/<action>',
Will be triggered, because it is after this rule
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
the shorter one will always be triggered first. So I would switch them around. Then test again.
Afterwards take care of the decrease parameter, are you sure you do not have a variable named like that?

Related

Yii2 change url structure

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

yii1 pagination with url-manager

i have problem with yii1 pagination
this is my url manage config:
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>/<slug:\w+>' => '<controller>/<action>',
'adminproduces/producelist/<slug:\w+>/<id:\w+>' => 'adminproduces/producelist',
'adminrequests/requestlist/<slug:\w+>/<id:\w+>' => 'adminrequests/requestlist',
'userrequest/neworder/<type:\w+>/<slug:\w+>' => 'userrequest/neworder',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
every thing ok ,but when i use pagination the created url is not matched with my urlmanger config
this my requested Url:
"/adminrequests/requestlist/digital/status10021"
that match with line 5 of url manger
....
in same page i use
<?php
// the pagination widget with some options to mess
$this->widget('CLinkPager', array('pages' => $pages));
?>
but when run project the link create this link for next pages
"adminrequests/requestlist/status10021?slug=digital&page=2"
insted of "adminrequests/requestlist/digital/status10021?page=2"
why?
can ypu help me please ?
when i manualy type "adminrequests/requestlist/digital/status10021?page=2" in address bar next page loaad correctly....
but pagination dosent make correct links...
Yii's CPagination has route property. You can use this for your need. Something like this:
$pages->route = "adminrequests/requestlist/digital/status10021";
You should usually set the part of url which is before starting pagination part.

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