I have a problem calling the url of my rest api in Yii2. I want to call a url like:
http://localhost/index-dev.php/myapi/collection/18
where 18 is the Id.
In my web.php config, I have the following code, with other settings from another programmers :
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => true,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['coding/nodes', 'coding/scales','myapi/collection']],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
],
],
when i call my URL, i get
Not Found (#404)
What am i doing wrong?
I had the same problem, you can disable the prural
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => true,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['coding/nodes', 'coding/scales','myapi/collection']],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'pluralize' => false,
],
],
You need to use the plural of your model class name in the URL for accessing a single model:
http://localhost/index-dev.php/myapi/collections/18
Take a look at the documentation of yii\rest\UrlRule:
The above code will create a whole set of URL rules supporting the following RESTful API endpoints:
'PUT,PATCH users/<id>' => 'user/update': update a user
'DELETE users/<id>' => 'user/delete': delete a user
'GET,HEAD users/<id>' => 'user/view': return the details/overview/options of a user
'POST users' => 'user/create': create a new user
'GET,HEAD users' => 'user/index': return a list/overview/options of users
'users/<id>' => 'user/options': process all unhandled verbs of a user
'users' => 'user/options': process all unhandled verbs of user collection
i suggest create separate module for API
and configure your UrlManager like...:)
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'pluralize'=>false,
'controller' => ['v1/country','v1/user'],
'tokens' => [
'{id}' => '<id:\\w+>'
],
'extraPatterns' => [
'POST register' => 'register', //from url
'GET exists'=>'exists',
'POST login'=>'login',
'POST follow'=>'follow',
'POST category'=>'category',
'PUT profile'=>'profile',
'PUT change_password'=>'change_password',
'PUT feed_interested'=>'feed_interested',
],
]
],
]
More detail # Create Rest Api
Related
I have catalogue with products. Urls for products looks like this domain.com/adv?id=14792.
I want to beautify urls like this domain.com/adv/14792.
In web.php I tried to like this
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
'adv/<id:\d+>' => 'site/adv',
'<alias:[\w-]+>' => 'site/<alias>',
],
],
and there is no result.
I tried another variant in rules block:
'rules' => [
'adv/<id:\d+>' => 'adv',
'<alias:[\w-]+>' => 'site/<alias>',
],
and urls start to look like I want. But on this link I get 404 page.
Please use action with adv so that it work.
'rules' => [
'adv/<id:\d+>' => 'adv/<add some action here>',
'<alias:[\w-]+>' => 'site/<alias>',
],
I solved my problem by myself. This is my urlmanager module now:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'adv/<id:\d+>' => 'site/adv',
'<alias:[\w-]+>' => 'site/<alias>',
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
],
this construction build url from https://example.com/adv?id=123456 to https://example.com/adv/123456.
In view link build with
Html::a($insideEl,Url::to(['adv','id' => $ladvert->id]))
and in siteController action is
actionAdv($id)
Thanx.
I am using nginx and yii2 trying to create a little sample crud rest API, and whenever I enable pretty urls I can't access my routes anmymore.
My config is as follows :
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'user',
'only' => ['delete', 'create', 'update', 'get'],
'patterns' => [
'PUT users/<id:\d+>' => 'user/update',
'DELETE users/<id:\d+>' => 'user/delete',
'GET users/<id:\d+>' => 'user/get',
'POST users' => 'user/create',
]
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'campaign',
'only' => ['delete', 'create', 'update', 'get'],
'patterns' => [
'PUT campaigns/<id:\d+>' => 'campaign/update',
'DELETE campaigns/<id:\d+>' => 'campaign/delete',
'GET campaigns/<id:\d+>' => 'campaign/get',
'POST campaigns' => 'campaign/create',
]
],
],
],
Note that when enabling this, I can access gii without any issue at /gii instead of /index.php?r=gii
When disabled, my routes work fine using index parameters. Also it's yii giving me 404 and not directly nginx.
EDIT: This url works /user/get?id=1
However this one does not /users/get/1, nor /user/get/1
A POST on /users will work, but not any of the other http requests
How can I enable clean-urls with multiple language in Yii2.
in - English
https://www.myfolder.com/en/restaurant/Bez_Gwiazdek-Warsaw
in - French
https://www.myfolder.com/fr/restaurant/Bez_Gwiazdek-Varsovie
in - Polish
https://www.myfolder.com/pl/Restauracja/Bez_Gwiazdek-Warszawa
please have a look at this repository https://github.com/codemix/yii2-localeurls#example-language-selection-widget
you need to update your urlManager configuration, something like this
[
'language' => 'en',
'components' => [
'urlManager' => [
'class' => 'codemix\localeurls\UrlManager',
'languages' => ['en','pl','fr'],
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<action:\w+>/<slug:[A-Za-z0-9 -_.]+>' => '<controller>/<action>',
],
],
...
]
This is how my url manager looks like:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'pluralize' => false,
'controller' => 'v1/user',
'extraPatterns' => [
'GET account' => 'account',
],
]
],
]
In order to make this call I can access this url:
localhost/project/api/web/v1/user/account
What I need to do is to remove controller name from url(user). I need to call the api at this url:
localhost/project/api/web/v1/account.
How can I achieve this? I tried a lot of tricks but none worked.
Thanks for your help.
You need to add following expression under rules
'v1/account' => 'v1/user/account',
I've problem with url in Yii.
when I enter URL like this,
http://localhost/yii_project/index.php/gii/default/login
it doesn't opens that page and it redirects to home page.
But when I enter url like this,
http://localhost/yii_project/index.php?r=gii/default/login
it open successfully.
I don't want to use index.php?r=. I want to display it index.php/gii/..
Change /protected/config/main.php as
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'search/<action:\w+>/'=>'dashboard/search/<action>',
'view/<action:\w+>/'=>'dashboard/view/<action>',
),
for more Info Hide index.php in yii
This is Yii1 or Yii2 you are using ?
For Yii2 Basic App (current version), open #app/config/web.php and add the following code:
This will result in this:
http://localhost/yii_project/gii/default/login
If you wanted the index.php included also then change a line to 'showScriptName' => true,
return [
... ,
'components' => [
.... ,
// below is the new section to include:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
//'suffix' => '.php',
//'cache' => 'cache',
//'scriptUrl' => '',
//'baseUrl' => '/',
//'hostInfo' => 'http://www.yourhost.com.au',
//'routeParam' => 'r',
'ruleConfig' => [
'class' => 'yii\web\UrlRule'
],
'rules' => array(
[
'pattern' => 'gii',
'route' => 'gii',
'suffix' => '',
],
[
'pattern' => '/<controller:\w+>/<action:\w+>',
'route' => 'gii/<controller>/<action>',
'suffix' => '',
],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
... ,
],
];