Yii2 pretty urls returning 404 - php

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

Related

yii\rest\UrlRule with "pluralize" rule is not working in Yii2 REST

I am unable to access default endpoints with pluralize option, view action is also not working
Case 1: Access without configure Module
'controllerNamespace' => 'api\controllers',
...
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'country',
'tokens' => [
'{id}' => '<id:\\w+>'
],
/*'pluralize'=>false,*/
],
]
http://localhost/api/web/countries Not working
http://localhost/api/web/country is working fine
http://localhost/api/web/country/1 is Not working
Case 2: Access via module v1
'modules' => [
'v1' => [
'basePath' => '#app/modules/v1',
'class' => 'api\modules\v1\Module'
]
],
...
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['country' => 'v1/country'],
'tokens' => [
'{id}' => '<id:\\w+>'
],
],
]
'pluralize' is not working completely and when access
v1/country & v1/country/12 both giving same result as index action (country list)
Your rule is incorrect you are missing the module name v1 in your rules
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/country',
'tokens' => [
'{id}' => '<id:\\w+>'
],
'extraPatterns' => [
'GET index' => 'index',
],
],
Now you can access it with
http://localhost/api/web/v1/countries
Note : in order to enable the POST request along with GET add it to the extra patterns like 'GET,POST index' => 'index',

Yii2 REST URL using slugs in place of id in view-slug method

I want to declare a method actionViewSlug($slug) in ScholarshipController in Yii2 REST api, My method is showing Not Found that too not in REST manner i.e. JSON.
Here is my Url Config for ScholarshipController
[
'class' => 'yii\rest\UrlRule',
'controller' => ['scholarship'],
'extraPatterns' => [
'POST filters' => 'filters',
'GET {slug}' => 'view-slug',
],
'tokens' => [
'{slug}' => '<slug>'
],
],
This is behaviors() function in ScholarshipController
public function behaviors()
{
return [
[
'class' => 'yii\filters\ContentNegotiator',
'only' => ['view', 'index', 'filters', 'view-slug'], // in a controller
// if in a module, use the following IDs for user actions
// 'only' => ['user/view', 'user/index']
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
],
'corsFilter' => [
'class' => \yii\filters\Cors::className(),
'cors' => [
'Origin' => ['*'],
'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
'Access-Control-Request-Headers' => ['*'],
'Access-Control-Allow-Credentials' => null,
'Access-Control-Max-Age' => 86400,
'Access-Control-Expose-Headers' => [],
],
],
];
}
Try this URL rule may it should help you.
But befor proceeding with this rule I want to clear you if you are using a module for rest API you must need to define your module ID too in rule either using prefix or direct
[
'class' => 'yii\rest\UrlRule',
'controller' => '<moduleID>/scholarships',
'tokens' => [
'{slug}' => '<slug>',
],
'extraPatterns' => [
'GET,HEAD {slug}' => 'view-slug',
]
],
and where is youe actionViewSlug($slug);
It is good if you share your controller code too. It's simple to use slug with different actions. One more thing here I would like to know why you are defining viewSlug action? Why you are not using the existing view action with slug?
Thanks

Yii2 rest api rewrite rules

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

How to do URL routing settings in Yii1?

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

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

Categories