Yii2 rest api rewrite rules - php

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

Related

YII2 sef urls construction

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.

Yii2 pretty urls returning 404

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

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

Yii2 Rest URL Routing

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

Yii2 stripping ending slash in Url::to() route when using params

I'm using Yii2 and have been using the Url::to method without any issue. Now I just tried it with params for the first time and it strips the ending slash from my route.
So normally, say I would do this:
Url::to('/my/route/', true);
// http://www.example.com/my/route/
But if I want to pass params, such as:
Url::to('['/my/route/', 'id' => 123, 'name' => 'larry']', true);
// http://www.example.com/my/route?id=123&name=larry
Is there any way to stop it doing this, if not, do I have any other options to get what I want?
My UrlManager rule:
'rules' => [
[
'pattern' => 'foo/<name:[0-9a-zA-Z\-]+>.<some_id:\d+>',
'route' => 'foo/index',
'suffix' => '/',
],
//.........
],
1) You can achieve that by adding suffix to corresponding url rule containing that route:
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
[
'pattern' => 'my/route',
'route' => 'my/route',
'suffix' => '/',
],
],
],
],
2) Globally it's available through urlManager property with the same name suffix.
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'suffix' => '/',
],
],
3) Alternative way of setting through component:
Yii::$app->urlManager->suffix = '/';
This one works for specific routes too, just add this line before calling Url::to();

Categories