Yii routing module problems - php

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

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

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

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.

yii2 url manager is not parsing second parameter

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?

url management in Yii 2

I have this url
http://example.com/index.php/controller_name/action_name?queryString=123
This url is working fine but when I am trying to use the queryString like in the old version of Yii
http://example.com/index.php/controller_name/action_name/queryString/123
I get an "unable to solve request" error.
I 've already enable prettyurl in my config file and the following url is working
http://example.com/index.php/controller_name/action_name.
My config looks like :
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'module/<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
],
],
What am I missing?
Unfortunately this feature didn't migrate to Yii2, you can still define such rules manually
'books/view/queryString/<queryString:\w+>' => 'books/view',
Link on github with this issue
Because of many client API and Oauth servers don't work without encode
Sam Dark answer

Categories